<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://teaching.healthtech.dtu.dk/22113/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Misc</id>
	<title>Example code - Misc - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://teaching.healthtech.dtu.dk/22113/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Misc"/>
	<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22113/index.php?title=Example_code_-_Misc&amp;action=history"/>
	<updated>2026-04-30T07:54:43Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22113/index.php?title=Example_code_-_Misc&amp;diff=33&amp;oldid=prev</id>
		<title>WikiSysop: Created page with &quot;__NOTOC__ == Files used in example == Our favorite data file: &#039;&#039;ex1.dat&#039;&#039;  == Closest to zero == There are a lot of numbers in this file. Which number is the closest to zero and where is it (row/column) ?&lt;br&gt; This is more complicated because there are both positive and negative numbers in the file.&lt;br&gt; In order to demonstrate some of the new functions/methods, I pull the entire file into memory. This problem could be solved by reading line-by-line and not keeping all the...&quot;</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22113/index.php?title=Example_code_-_Misc&amp;diff=33&amp;oldid=prev"/>
		<updated>2024-03-06T14:00:26Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__NOTOC__ == Files used in example == Our favorite data file: &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039;  == Closest to zero == There are a lot of numbers in this file. Which number is the closest to zero and where is it (row/column) ?&amp;lt;br&amp;gt; This is more complicated because there are both positive and negative numbers in the file.&amp;lt;br&amp;gt; In order to demonstrate some of the new functions/methods, I pull the entire file into memory. This problem could be solved by reading line-by-line and not keeping all the...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__NOTOC__&lt;br /&gt;
== Files used in example ==&lt;br /&gt;
Our favorite data file: &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Closest to zero ==&lt;br /&gt;
There are a lot of numbers in this file. Which number is the closest to zero and where is it (row/column) ?&amp;lt;br&amp;gt;&lt;br /&gt;
This is more complicated because there are both positive and negative numbers in the file.&amp;lt;br&amp;gt;&lt;br /&gt;
In order to demonstrate some of the new functions/methods, I pull the entire file into memory. This problem could be solved by reading line-by-line and not keeping all the data.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
# Read file, keep track of where numbers are&lt;br /&gt;
numbers = list()&lt;br /&gt;
with open(&amp;quot;ex1.dat&amp;quot;, &amp;quot;r&amp;quot;) as number_file:&lt;br /&gt;
    line_count = 0&lt;br /&gt;
    for line in number_file:&lt;br /&gt;
        for field_pos, field_value in enumerate(line.split()):&lt;br /&gt;
            try:&lt;br /&gt;
                number = float(field_value)&lt;br /&gt;
                numbers.append((line_count, field_pos, number))&lt;br /&gt;
            except ValueError:&lt;br /&gt;
                pass&lt;br /&gt;
        line_count += 1&lt;br /&gt;
if len(numbers) &amp;gt; 0:&lt;br /&gt;
    minimum = min(numbers, key=lambda x: abs(x[2]))&lt;br /&gt;
    # Display Human numbers.&lt;br /&gt;
    print(minimum[2], &amp;#039;in line&amp;#039;, minimum[0]+1, &amp;#039;column&amp;#039;, minimum[1]+1, &amp;#039;is closest to 0.&amp;#039;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;No numbers in the file.&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how I actually don&amp;#039;t care if all the fields are numbers, nor if there is the same amount of fields on every row.&lt;br /&gt;
By changing &amp;#039;&amp;#039;&amp;#039;min&amp;#039;&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;#039;max&amp;#039;&amp;#039;&amp;#039;  I can find the number furthest away from 0.&lt;br /&gt;
The runtime is linear to the amount of numbers/words in the file, as I am not doing any sorting.&lt;/div&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
</feed>