<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?action=history&amp;feed=atom&amp;title=Comprehension%2C_generators%2C_iteration</id>
	<title>Comprehension, generators, iteration - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?action=history&amp;feed=atom&amp;title=Comprehension%2C_generators%2C_iteration"/>
	<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;action=history"/>
	<updated>2026-07-03T19:48:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=222&amp;oldid=prev</id>
		<title>WikiSysop: /* Exercises to be handed in */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=222&amp;oldid=prev"/>
		<updated>2026-03-12T21:30:54Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Exercises to be handed in&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 23:30, 12 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l29&quot;&gt;Line 29:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 29:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# This exercise takes place &amp;#039;&amp;#039;&amp;#039;after&amp;#039;&amp;#039;&amp;#039; exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&amp;#039;s last lesson]. The exercises are repeated as extra exercise below if you want to do that work, but they are most there for reference.&amp;lt;br&amp;gt;The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; file after a normalization (done in extra exercise 2) followed by an linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; created in extra exercise 2 (original 5) as input. The tab-separated input file &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; has an &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; (slope) and a &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; (intersection) - one AB pair for each number column in the &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; file. For each line in &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; you first linear transform the numbers according to the A &amp;amp; B in &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# This exercise takes place &amp;#039;&amp;#039;&amp;#039;after&amp;#039;&amp;#039;&amp;#039; exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&amp;#039;s last lesson]. The exercises are repeated as extra exercise below if you want to do that work, but they are most there for reference.&amp;lt;br&amp;gt;The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; file after a normalization (done in extra exercise 2) followed by an linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; created in extra exercise 2 (original 5) as input. The tab-separated input file &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; has an &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; (slope) and a &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; (intersection) - one AB pair for each number column in the &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; file. For each line in &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; you first linear transform the numbers according to the A &amp;amp; B in &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &#039;&#039;&#039;moving_avg(List_of_numbers, Window_size)&#039;&#039;&#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &#039;&#039;ex1.dat&#039;&#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list. Check the youtube video above, if you &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;donøt &lt;/del&gt;get the moving window concept.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &#039;&#039;&#039;moving_avg(List_of_numbers, Window_size)&#039;&#039;&#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &#039;&#039;ex1.dat&#039;&#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list. Check the youtube video above, if you &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;don&#039;t &lt;/ins&gt;get the moving window concept.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Changing the previous exercise: Make a &amp;#039;&amp;#039;&amp;#039;find_trend(List_of_numbers, Minimum_trend_size)&amp;#039;&amp;#039;&amp;#039; generator, which return a tuple &amp;#039;&amp;#039;&amp;#039;(Position_Start, Size, Direction)&amp;#039;&amp;#039;&amp;#039; of where and how big the trends in List_of_numbers are. &amp;#039;&amp;#039;&amp;#039;Direction&amp;#039;&amp;#039;&amp;#039; is 0 or 1 as you want to know which direction the trend is going. &amp;#039;&amp;#039;&amp;#039;Position_start&amp;#039;&amp;#039;&amp;#039; is the position in the (zero-based) list, where the trend starts. &amp;#039;&amp;#039;&amp;#039;Size&amp;#039;&amp;#039;&amp;#039; is how long the trend of ascending/descending numbers is. This is surprisingly difficult. Test with a simple file of your own making to check your results.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Changing the previous exercise: Make a &amp;#039;&amp;#039;&amp;#039;find_trend(List_of_numbers, Minimum_trend_size)&amp;#039;&amp;#039;&amp;#039; generator, which return a tuple &amp;#039;&amp;#039;&amp;#039;(Position_Start, Size, Direction)&amp;#039;&amp;#039;&amp;#039; of where and how big the trends in List_of_numbers are. &amp;#039;&amp;#039;&amp;#039;Direction&amp;#039;&amp;#039;&amp;#039; is 0 or 1 as you want to know which direction the trend is going. &amp;#039;&amp;#039;&amp;#039;Position_start&amp;#039;&amp;#039;&amp;#039; is the position in the (zero-based) list, where the trend starts. &amp;#039;&amp;#039;&amp;#039;Size&amp;#039;&amp;#039;&amp;#039; is how long the trend of ascending/descending numbers is. This is surprisingly difficult. Test with a simple file of your own making to check your results.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=221&amp;oldid=prev</id>
		<title>WikiSysop: /* Exercises to be handed in */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=221&amp;oldid=prev"/>
		<updated>2026-03-12T21:30:40Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Exercises to be handed in&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 23:30, 12 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l29&quot;&gt;Line 29:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 29:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# This exercise takes place &amp;#039;&amp;#039;&amp;#039;after&amp;#039;&amp;#039;&amp;#039; exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&amp;#039;s last lesson]. The exercises are repeated as extra exercise below if you want to do that work, but they are most there for reference.&amp;lt;br&amp;gt;The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; file after a normalization (done in extra exercise 2) followed by an linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; created in extra exercise 2 (original 5) as input. The tab-separated input file &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; has an &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; (slope) and a &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; (intersection) - one AB pair for each number column in the &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; file. For each line in &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; you first linear transform the numbers according to the A &amp;amp; B in &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# This exercise takes place &amp;#039;&amp;#039;&amp;#039;after&amp;#039;&amp;#039;&amp;#039; exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&amp;#039;s last lesson]. The exercises are repeated as extra exercise below if you want to do that work, but they are most there for reference.&amp;lt;br&amp;gt;The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; file after a normalization (done in extra exercise 2) followed by an linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; created in extra exercise 2 (original 5) as input. The tab-separated input file &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; has an &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; (slope) and a &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; (intersection) - one AB pair for each number column in the &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; file. For each line in &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; you first linear transform the numbers according to the A &amp;amp; B in &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &#039;&#039;&#039;moving_avg(List_of_numbers, Window_size)&#039;&#039;&#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &#039;&#039;ex1.dat&#039;&#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &#039;&#039;&#039;moving_avg(List_of_numbers, Window_size)&#039;&#039;&#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &#039;&#039;ex1.dat&#039;&#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;. Check the youtube video above, if you donøt get the moving window concept&lt;/ins&gt;.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Changing the previous exercise: Make a &amp;#039;&amp;#039;&amp;#039;find_trend(List_of_numbers, Minimum_trend_size)&amp;#039;&amp;#039;&amp;#039; generator, which return a tuple &amp;#039;&amp;#039;&amp;#039;(Position_Start, Size, Direction)&amp;#039;&amp;#039;&amp;#039; of where and how big the trends in List_of_numbers are. &amp;#039;&amp;#039;&amp;#039;Direction&amp;#039;&amp;#039;&amp;#039; is 0 or 1 as you want to know which direction the trend is going. &amp;#039;&amp;#039;&amp;#039;Position_start&amp;#039;&amp;#039;&amp;#039; is the position in the (zero-based) list, where the trend starts. &amp;#039;&amp;#039;&amp;#039;Size&amp;#039;&amp;#039;&amp;#039; is how long the trend of ascending/descending numbers is. This is surprisingly difficult. Test with a simple file of your own making to check your results.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Changing the previous exercise: Make a &amp;#039;&amp;#039;&amp;#039;find_trend(List_of_numbers, Minimum_trend_size)&amp;#039;&amp;#039;&amp;#039; generator, which return a tuple &amp;#039;&amp;#039;&amp;#039;(Position_Start, Size, Direction)&amp;#039;&amp;#039;&amp;#039; of where and how big the trends in List_of_numbers are. &amp;#039;&amp;#039;&amp;#039;Direction&amp;#039;&amp;#039;&amp;#039; is 0 or 1 as you want to know which direction the trend is going. &amp;#039;&amp;#039;&amp;#039;Position_start&amp;#039;&amp;#039;&amp;#039; is the position in the (zero-based) list, where the trend starts. &amp;#039;&amp;#039;&amp;#039;Size&amp;#039;&amp;#039;&amp;#039; is how long the trend of ascending/descending numbers is. This is surprisingly difficult. Test with a simple file of your own making to check your results.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=220&amp;oldid=prev</id>
		<title>WikiSysop: /* Required course material for the lesson */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=220&amp;oldid=prev"/>
		<updated>2026-03-12T21:29:06Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Required course material for the lesson&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 23:29, 12 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l11&quot;&gt;Line 11:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=ff23a72f-a1ee-46fc-a434-af2701281405 Iteration in detail, use of lambda function, libraries]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=ff23a72f-a1ee-46fc-a434-af2701281405 Iteration in detail, use of lambda function, libraries]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=83c5ef87-3ed8-4069-b691-af130047ec9f How to parse bio files with many entries]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=83c5ef87-3ed8-4069-b691-af130047ec9f How to parse bio files with many entries]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Youtube: [https://www.youtube.com/watch?v=uLfQ4Jbvo-M Showing moving window concept] Moving/sliding/rolling window are all names for the same concept.&amp;lt;br&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Resource: [[Example code - Comprehension]]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Resource: [[Example code - Comprehension]]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Resource: [[Example code - Misc]]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Resource: [[Example code - Misc]]&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=197&amp;oldid=prev</id>
		<title>WikiSysop: /* Exercises to be handed in */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=197&amp;oldid=prev"/>
		<updated>2026-03-04T10:25:46Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Exercises to be handed in&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:25, 4 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l26&quot;&gt;Line 26:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 26:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises to be handed in ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises to be handed in ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&quot;#AA00FF&quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &#039;&#039;mat1.dat&#039;&#039; and &#039;&#039;mat2.dat&#039;&#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&quot;#AA00FF&quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &#039;&#039;mat1.dat&#039;&#039; and &#039;&#039;mat2.dat&#039;&#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&lt;/ins&gt;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# This exercise takes place &#039;&#039;&#039;after&#039;&#039;&#039; exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&#039;s last lesson]. The exercises are repeated as extra exercise below if you want to do that work, but they are most there for reference.&amp;lt;br&amp;gt;The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &#039;&#039;dna-array.dat&#039;&#039; file after a normalization (done in extra exercise 2) followed by an linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &#039;&#039;dna-array-norm.dat&#039;&#039; created in extra exercise 2 (original 5) as input. The tab-separated input file &#039;&#039;lineartransform.dat&#039;&#039; has an &#039;&#039;&#039;A&#039;&#039;&#039; (slope) and a &#039;&#039;&#039;B&#039;&#039;&#039; (intersection) - one AB pair for each number column in the &#039;&#039;dna-array-norm.dat&#039;&#039; file. For each line in &#039;&#039;dna-array-norm.dat&#039;&#039; you first linear transform the numbers according to the A &amp;amp; B in &#039;&#039;lineartransform.dat&#039;&#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# This exercise takes place &#039;&#039;&#039;after&#039;&#039;&#039; exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&#039;s last lesson]. The exercises are repeated as extra exercise below if you want to do that work, but they are most there for reference.&amp;lt;br&amp;gt;The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &#039;&#039;dna-array.dat&#039;&#039; file after a normalization (done in extra exercise 2) followed by an linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &#039;&#039;dna-array-norm.dat&#039;&#039; created in extra exercise 2 (original 5) as input. The tab-separated input file &#039;&#039;lineartransform.dat&#039;&#039; has an &#039;&#039;&#039;A&#039;&#039;&#039; (slope) and a &#039;&#039;&#039;B&#039;&#039;&#039; (intersection) - one AB pair for each number column in the &#039;&#039;dna-array-norm.dat&#039;&#039; file. For each line in &#039;&#039;dna-array-norm.dat&#039;&#039; you first linear transform the numbers according to the A &amp;amp; B in &#039;&#039;lineartransform.dat&#039;&#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &#039;&#039;&#039;moving_avg(List_of_numbers, Window_size)&#039;&#039;&#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &#039;&#039;ex1.dat&#039;&#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &#039;&#039;&#039;moving_avg(List_of_numbers, Window_size)&#039;&#039;&#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &#039;&#039;ex1.dat&#039;&#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &#039;&#039;&#039;trend(List_of_numbers)&#039;&#039;&#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&#039;s or 1&#039;s in the generator output is a trend in the data. Check with &#039;&#039;ex1.dat&#039;&#039; (load same way a previous exercise) or another file of your choosing.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &#039;&#039;&#039;trend(List_of_numbers)&#039;&#039;&#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&#039;s or 1&#039;s in the generator output is a trend in the data. Check with &#039;&#039;ex1.dat&#039;&#039; (load same way a previous exercise) or another file of your choosing.&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Changing the previous exercise: Make a &#039;&#039;&#039;find_trend(List_of_numbers, Minimum_trend_size)&#039;&#039;&#039; generator, which return a tuple &#039;&#039;&#039;(Position_Start, Size, Direction)&#039;&#039;&#039; of where and how big the trends in List_of_numbers are. &#039;&#039;&#039;Direction&#039;&#039;&#039; is 0 or 1 as you want to know which direction the trend is going. &#039;&#039;&#039;Position_start&#039;&#039;&#039; is the position in the (zero-based) list, where the trend starts. &#039;&#039;&#039;Size&#039;&#039;&#039; is how long the trend of ascending/descending numbers is. This is surprisingly difficult. Test with a simple file of your own making to check your results.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Changing the previous exercise: Make a &#039;&#039;&#039;find_trend(List_of_numbers, Minimum_trend_size)&#039;&#039;&#039; generator, which return a tuple &#039;&#039;&#039;(Position_Start, Size, Direction)&#039;&#039;&#039; of where and how big the trends in List_of_numbers are. &#039;&#039;&#039;Direction&#039;&#039;&#039; is 0 or 1 as you want to know which direction the trend is going. &#039;&#039;&#039;Position_start&#039;&#039;&#039; is the position in the (zero-based) list, where the trend starts. &#039;&#039;&#039;Size&#039;&#039;&#039; is how long the trend of ascending/descending numbers is. This is surprisingly difficult. Test with a simple file of your own making to check your results.&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a generator &amp;#039;&amp;#039;&amp;#039;combinations()&amp;#039;&amp;#039;&amp;#039;, that takes a list of strings as input, e.g. &amp;#039;&amp;#039;&amp;#039;combinations([&amp;quot;GAVIL&amp;quot;, &amp;quot;ST&amp;quot;, &amp;quot;NQ&amp;quot;, &amp;quot;FWY&amp;quot;, &amp;quot;D&amp;quot;, &amp;quot;HKR&amp;quot;])&amp;#039;&amp;#039;&amp;#039;, and generates all possible combinations. A combination is formed by choosing 1 letter from the first string, 1 letter from the second string, and so forth, in that order, until a letter from all strings is chosen. The input list can have any number of strings and the strings can have any length (greater than 0). There must be NO REPEATS - random is not an acceptable library to use. As is obvious, the example has 5*2*2*3*1*3 = 180 different combinations, the first being GSNFDH. Print them all on the screen. If your input is [&amp;#039;0123456789&amp;#039;, &amp;#039;0123456789&amp;#039;, &amp;#039;0123456789&amp;#039;], then you will print the numbers from 000 to 999. Hint: A list of counters, 1 per string, could be useful in iterating through the combinations.&amp;lt;br&amp;gt;When can such a generator be useful? If you want to generate a list of antigens, which needs certain amino acids to be in certain positions.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a generator &amp;#039;&amp;#039;&amp;#039;combinations()&amp;#039;&amp;#039;&amp;#039;, that takes a list of strings as input, e.g. &amp;#039;&amp;#039;&amp;#039;combinations([&amp;quot;GAVIL&amp;quot;, &amp;quot;ST&amp;quot;, &amp;quot;NQ&amp;quot;, &amp;quot;FWY&amp;quot;, &amp;quot;D&amp;quot;, &amp;quot;HKR&amp;quot;])&amp;#039;&amp;#039;&amp;#039;, and generates all possible combinations. A combination is formed by choosing 1 letter from the first string, 1 letter from the second string, and so forth, in that order, until a letter from all strings is chosen. The input list can have any number of strings and the strings can have any length (greater than 0). There must be NO REPEATS - random is not an acceptable library to use. As is obvious, the example has 5*2*2*3*1*3 = 180 different combinations, the first being GSNFDH. Print them all on the screen. If your input is [&amp;#039;0123456789&amp;#039;, &amp;#039;0123456789&amp;#039;, &amp;#039;0123456789&amp;#039;], then you will print the numbers from 000 to 999. Hint: A list of counters, 1 per string, could be useful in iterating through the combinations.&amp;lt;br&amp;gt;When can such a generator be useful? If you want to generate a list of antigens, which needs certain amino acids to be in certain positions.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=196&amp;oldid=prev</id>
		<title>WikiSysop: /* Exercises to be handed in */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=196&amp;oldid=prev"/>
		<updated>2026-03-04T10:20:25Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Exercises to be handed in&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:20, 4 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l27&quot;&gt;Line 27:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 27:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises to be handed in ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises to be handed in ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &#039;&#039;dna-array.dat&#039;&#039; file after a linear transformation of the numbers in the columns&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;. You should (re)visist exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&#039;s last lesson]&lt;/del&gt;. In order to not start from the beginning, use the file &#039;&#039;dna-array-norm.dat&#039;&#039; created in exercise 5 as input&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;, both are supplied if you want to do extra work&lt;/del&gt;. The &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;other &lt;/del&gt;tab-separated input file &#039;&#039;lineartransform.dat&#039;&#039; has an &#039;&#039;&#039;A&#039;&#039;&#039; (slope) and a &#039;&#039;&#039;B&#039;&#039;&#039; (intersection) - one AB pair for each number column in the &#039;&#039;dna-array-norm.dat&#039;&#039; file. For each line in &#039;&#039;dna-array-norm.dat&#039;&#039; you first linear transform the numbers according to the A &amp;amp; B in &#039;&#039;lineartransform.dat&#039;&#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;This exercise takes place &#039;&#039;&#039;after&#039;&#039;&#039; exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&#039;s last lesson]. The exercises are repeated as extra exercise below if you want to do that work, but they are most there for reference.&amp;lt;br&amp;gt;&lt;/ins&gt;The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &#039;&#039;dna-array.dat&#039;&#039; file after a &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;normalization (done in extra exercise 2) followed by an &lt;/ins&gt;linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &#039;&#039;dna-array-norm.dat&#039;&#039; created in &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;extra &lt;/ins&gt;exercise &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;2 (original &lt;/ins&gt;5&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;) &lt;/ins&gt;as input. The tab-separated input file &#039;&#039;lineartransform.dat&#039;&#039; has an &#039;&#039;&#039;A&#039;&#039;&#039; (slope) and a &#039;&#039;&#039;B&#039;&#039;&#039; (intersection) - one AB pair for each number column in the &#039;&#039;dna-array-norm.dat&#039;&#039; file. For each line in &#039;&#039;dna-array-norm.dat&#039;&#039; you first linear transform the numbers according to the A &amp;amp; B in &#039;&#039;lineartransform.dat&#039;&#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &amp;#039;&amp;#039;&amp;#039;moving_avg(List_of_numbers, Window_size)&amp;#039;&amp;#039;&amp;#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &amp;#039;&amp;#039;&amp;#039;moving_avg(List_of_numbers, Window_size)&amp;#039;&amp;#039;&amp;#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=195&amp;oldid=prev</id>
		<title>WikiSysop: /* Exercises for extra practice */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=195&amp;oldid=prev"/>
		<updated>2026-03-04T10:09:29Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Exercises for extra practice&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:09, 4 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l36&quot;&gt;Line 36:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 36:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Study the file &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; a bit. This is real DNA array data taken from a number of persons, some controls and some suffering from colon cancer. If you look at the second line there is a lot of 0 and 1. A &amp;#039;0&amp;#039; means that values in that column are from a cancer patient and a &amp;#039;1&amp;#039; means data are from a control (healthy person). The data are all log(intensity), i.e. the logarithm of the measured intensity of the relevant spot on the dna-chip. The data in this file will be used in coming exercises. The data/columns are tab separated. The second item on each line is the accession number for that particular gene.&amp;lt;br&amp;gt;Now make a main function that extracts data from one file and saves it in another, given the accession number, input file &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; and output file &amp;#039;&amp;#039;column.tab&amp;#039;&amp;#039;. Search in the file for the data concerning that accession number. If it does not find it (you gave a wrong accession no), complain and stop. Otherwise it shall display the data in two tab separated columns. First column shall be the data from the cancer patients, second column for the controls. There are not the same number of sick and healthy people - be able to handle that.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Study the file &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; a bit. This is real DNA array data taken from a number of persons, some controls and some suffering from colon cancer. If you look at the second line there is a lot of 0 and 1. A &amp;#039;0&amp;#039; means that values in that column are from a cancer patient and a &amp;#039;1&amp;#039; means data are from a control (healthy person). The data are all log(intensity), i.e. the logarithm of the measured intensity of the relevant spot on the dna-chip. The data in this file will be used in coming exercises. The data/columns are tab separated. The second item on each line is the accession number for that particular gene.&amp;lt;br&amp;gt;Now make a main function that extracts data from one file and saves it in another, given the accession number, input file &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; and output file &amp;#039;&amp;#039;column.tab&amp;#039;&amp;#039;. Search in the file for the data concerning that accession number. If it does not find it (you gave a wrong accession no), complain and stop. Otherwise it shall display the data in two tab separated columns. First column shall be the data from the cancer patients, second column for the controls. There are not the same number of sick and healthy people - be able to handle that.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# The numbers in the input file &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; should be normalized between 0 and 1 for each line with an accession number, i.e. normalization only for the individual line - not across the data set. Write the result out in the file &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039;, but NOT the control lines, i.e. lines where the annotation says &amp;#039;control&amp;#039;. The resulting file will be similar to the original, but control lines are removed and the numbers are different. The problem can (and should) be solved one line at a time.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# The numbers in the input file &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; should be normalized between 0 and 1 for each line with an accession number, i.e. normalization only for the individual line - not across the data set. Write the result out in the file &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039;, but NOT the control lines, i.e. lines where the annotation says &amp;#039;control&amp;#039;. The resulting file will be similar to the original, but control lines are removed and the numbers are different. The problem can (and should) be solved one line at a time.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# Read the file &#039;&#039;dna-array-norm.dat&#039;&#039; and transform all the numbers less than 0.5 to 0, and numbers at 0.5 or more to 1. Now for each line/accession calculate the average of the control group numbers and the cancer group numbers. If the two averages are more than 0.4 from each other, this is considered significant and the accession should be saved in the file &#039;&#039;regulation.txt&#039;&#039; along with a message &#039;&#039;&#039;up&#039;&#039;&#039; or &#039;&#039;&#039;down&#039;&#039;&#039; if it is an up regulation or a down regulation of the cancer group compared to the control. That means each output line looks like &quot;H80240 up&quot; or &quot;H34534 down&quot;.&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=194&amp;oldid=prev</id>
		<title>WikiSysop: /* Exercises for extra practice */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=194&amp;oldid=prev"/>
		<updated>2026-03-04T10:08:50Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Exercises for extra practice&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:08, 4 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l34&quot;&gt;Line 34:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 34:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises for extra practice ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises for extra practice ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# Study the file &#039;&#039;dna-array.dat&#039;&#039; a bit. This is real DNA array data taken from a number of persons, some controls and some suffering from colon cancer. If you look at the second line there is a lot of 0 and 1. A &#039;0&#039; means that values in that column are from a cancer patient and a &#039;1&#039; means data are from a control (healthy person). The data are all log(intensity), i.e. the logarithm of the measured intensity of the relevant spot on the dna-chip. The data in this file will be used in coming exercises. The data/columns are tab separated. The second item on each line is the accession number for that particular gene.&amp;lt;br&amp;gt;Now make a main function that extracts data from one file and saves it in another, given the accession number, input file &#039;&#039;dna-array.dat&#039;&#039; and output file &#039;&#039;column.tab&#039;&#039;. Search in the file for the data concerning that accession number. If it does not find it (you gave a wrong accession no), complain and stop. Otherwise it shall display the data in two tab separated columns. First column shall be the data from the cancer patients, second column for the controls. There are not the same number of sick and healthy people - be able to handle that.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# The numbers in the input file &#039;&#039;dna-array.dat&#039;&#039; should be normalized between 0 and 1 for each line with an accession number, i.e. normalization only for the individual line - not across the data set. Write the result out in the file &#039;&#039;dna-array-norm.dat&#039;&#039;, but NOT the control lines, i.e. lines where the annotation says &#039;control&#039;. The resulting file will be similar to the original, but control lines are removed and the numbers are different. The problem can (and should) be solved one line at a time.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# Read the file &#039;&#039;dna-array-norm.dat&#039;&#039; and transform all the numbers less than 0.5 to 0, and numbers at 0.5 or more to 1. Now for each line/accession calculate the average of the control group numbers and the cancer group numbers. If the two averages are more than 0.4 from each other, this is considered significant and the accession should be saved in the file &#039;&#039;regulation.txt&#039;&#039; along with a message &#039;&#039;&#039;up&#039;&#039;&#039; or &#039;&#039;&#039;down&#039;&#039;&#039; if it is an up regulation or a down regulation of the cancer group compared to the control. That means each output line looks like &quot;H80240 up&quot; or &quot;H34534 down&quot;.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=193&amp;oldid=prev</id>
		<title>WikiSysop: /* Exercises to be handed in */</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=193&amp;oldid=prev"/>
		<updated>2026-03-04T10:06:49Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Exercises to be handed in&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:06, 4 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l27&quot;&gt;Line 27:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 27:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises to be handed in ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Exercises to be handed in ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &#039;&#039;dna-array.dat&#039;&#039; file after a linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &#039;&#039;dna-array-norm.dat&#039;&#039; created in exercise &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;4 in [[Advanced Data Structures and New Data Types]] &lt;/del&gt;as input. The other tab-separated input file &#039;&#039;lineartransform.dat&#039;&#039; has an &#039;&#039;&#039;A&#039;&#039;&#039; (slope) and a &#039;&#039;&#039;B&#039;&#039;&#039; (intersection) - one AB pair for each number column in the &#039;&#039;dna-array-norm.dat&#039;&#039; file. For each line in &#039;&#039;dna-array-norm.dat&#039;&#039; you first linear transform the numbers according to the A &amp;amp; B in &#039;&#039;lineartransform.dat&#039;&#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &#039;&#039;dna-array.dat&#039;&#039; file after a linear transformation of the numbers in the columns&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;. You should (re)visist exercise 4 &amp;amp; 5 in [https://teaching.healthtech.dtu.dk/22116/index.php/Python_object_model course 22116&#039;s last lesson]&lt;/ins&gt;. In order to not start from the beginning, use the file &#039;&#039;dna-array-norm.dat&#039;&#039; created in exercise &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;5 &lt;/ins&gt;as input&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;, both are supplied if you want to do extra work&lt;/ins&gt;. The other tab-separated input file &#039;&#039;lineartransform.dat&#039;&#039; has an &#039;&#039;&#039;A&#039;&#039;&#039; (slope) and a &#039;&#039;&#039;B&#039;&#039;&#039; (intersection) - one AB pair for each number column in the &#039;&#039;dna-array-norm.dat&#039;&#039; file. For each line in &#039;&#039;dna-array-norm.dat&#039;&#039; you first linear transform the numbers according to the A &amp;amp; B in &#039;&#039;lineartransform.dat&#039;&#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &amp;#039;&amp;#039;&amp;#039;moving_avg(List_of_numbers, Window_size)&amp;#039;&amp;#039;&amp;#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a moving average generator: &amp;#039;&amp;#039;&amp;#039;moving_avg(List_of_numbers, Window_size)&amp;#039;&amp;#039;&amp;#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=94&amp;oldid=prev</id>
		<title>WikiSysop: Created page with &quot;__NOTOC__ {| width=500  style=&quot;font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;&quot; |Previous: Functions, namespace, memory management |Next: Beginning classes |} == Required course material for the lesson == Powerpoint: [https://teaching.healthtech.dtu.dk/material/22118/22118_06-Comprehension.ppt Comprehension, generators &amp; iteration]&lt;br&gt; Powerpoint: [https://teaching.healthtech.dtu.dk/material/22118/22118_06-Random.ppt Randomness]&lt;br&gt; Video: [ht...&quot;</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22118/index.php?title=Comprehension,_generators,_iteration&amp;diff=94&amp;oldid=prev"/>
		<updated>2026-01-27T08:21:55Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__NOTOC__ {| width=500  style=&amp;quot;font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;&amp;quot; |Previous: &lt;a href=&quot;/22118/index.php/Functions,_namespace,_memory_management&quot; title=&quot;Functions, namespace, memory management&quot;&gt;Functions, namespace, memory management&lt;/a&gt; |Next: &lt;a href=&quot;/22118/index.php/Beginning_classes&quot; title=&quot;Beginning classes&quot;&gt;Beginning classes&lt;/a&gt; |} == Required course material for the lesson == Powerpoint: [https://teaching.healthtech.dtu.dk/material/22118/22118_06-Comprehension.ppt Comprehension, generators &amp;amp; iteration]&amp;lt;br&amp;gt; Powerpoint: [https://teaching.healthtech.dtu.dk/material/22118/22118_06-Random.ppt Randomness]&amp;lt;br&amp;gt; Video: [ht...&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;
{| width=500  style=&amp;quot;font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;&amp;quot;&lt;br /&gt;
|Previous: [[Functions, namespace, memory management]]&lt;br /&gt;
|Next: [[Beginning classes]]&lt;br /&gt;
|}&lt;br /&gt;
== Required course material for the lesson ==&lt;br /&gt;
Powerpoint: [https://teaching.healthtech.dtu.dk/material/22118/22118_06-Comprehension.ppt Comprehension, generators &amp;amp; iteration]&amp;lt;br&amp;gt;&lt;br /&gt;
Powerpoint: [https://teaching.healthtech.dtu.dk/material/22118/22118_06-Random.ppt Randomness]&amp;lt;br&amp;gt;&lt;br /&gt;
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=ef476c74-cc82-4478-afd2-af270128c92f Comprehension]&amp;lt;br&amp;gt;&lt;br /&gt;
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=ad2b345c-4c9f-4fbb-8593-af270128ae40 Generators]&amp;lt;br&amp;gt;&lt;br /&gt;
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=ff23a72f-a1ee-46fc-a434-af2701281405 Iteration in detail, use of lambda function, libraries]&amp;lt;br&amp;gt;&lt;br /&gt;
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=83c5ef87-3ed8-4069-b691-af130047ec9f How to parse bio files with many entries]&amp;lt;br&amp;gt;&lt;br /&gt;
Resource: [[Example code - Comprehension]]&amp;lt;br&amp;gt;&lt;br /&gt;
Resource: [[Example code - Misc]]&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Video: [https://video.dtu.dk/media/22110-lesson11-LiveCoding/0_0l5ifl0e Live Coding 1]&amp;lt;br&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=56f5c2c8-a3fe-4847-bb94-af2701286138 Live Coding 2]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Subjects covered ==&lt;br /&gt;
&amp;#039;&amp;#039;Comprehension&amp;#039;&amp;#039;, which is a way of manipulation/selecting data with a hidden loop.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;Lambda&amp;#039;&amp;#039;, the small anonymous function.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;Generators&amp;#039;&amp;#039;, which is like a function with memory of previous calls.&amp;lt;br&amp;gt;&lt;br /&gt;
More theoretical iteration.&amp;lt;br&amp;gt;&lt;br /&gt;
On the nature of randomness in the computer.&lt;br /&gt;
&lt;br /&gt;
== Exercises to be handed in ==&lt;br /&gt;
# &amp;lt;font color=&amp;quot;#AA00FF&amp;quot;&amp;gt;Make a program that calculates the product of two matrices and prints it on the screen (which is STDOUT, remember unix). The matrices are in the files &amp;#039;&amp;#039;mat1.dat&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mat2.dat&amp;#039;&amp;#039;. Numbers in the files are tab separated. A matrix should be stored as an list of lists.&amp;lt;br&amp;gt;Advice: The program should have a function that reads a matrix from a given file (to be used twice), a function that calculates the product, and a function that prints a matrix. This way ensures that your program is easy to change to other forms of matrix calculations. Here are two links to the definition of matrix multiplication.&amp;lt;br&amp;gt;[https://www.mathsisfun.com/algebra/matrix-multiplying.html Math is Fun]&amp;lt;br&amp;gt;[http://mathworld.wolfram.com/MatrixMultiplication.html Math world]&amp;lt;/font&amp;gt;&lt;br /&gt;
# The purpose of this exercise is to find the 10 genes that has the biggest difference in expression between cancer and control patients in the &amp;#039;&amp;#039;dna-array.dat&amp;#039;&amp;#039; file after a linear transformation of the numbers in the columns. In order to not start from the beginning, use the file &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; created in exercise 4 in [[Advanced Data Structures and New Data Types]] as input. The other tab-separated input file &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; has an &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; (slope) and a &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; (intersection) - one AB pair for each number column in the &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; file. For each line in &amp;#039;&amp;#039;dna-array-norm.dat&amp;#039;&amp;#039; you first linear transform the numbers according to the A &amp;amp; B in &amp;#039;&amp;#039;lineartransform.dat&amp;#039;&amp;#039; - first number uses first AB pair, second number uses second AB pair, and so forth. If your number is X, then the transformed number is A*X+B. When the entire line is transformed, you calculate the average of the cancer patients and the average of the controls. From that, find the 10 genes with the biggest difference in expression. There are a number of ways, but a simple one is to create a list of tuples with every tuple consisting of (gene name, cancer average, control average), and then sort the list according to the difference in cancer and control average. Using a lambda function when sorting springs to mind. Display the top 10 in the sorted list.&lt;br /&gt;
# Make a moving average generator: &amp;#039;&amp;#039;&amp;#039;moving_avg(List_of_numbers, Window_size)&amp;#039;&amp;#039;&amp;#039;. The generator calculates the average number in a window moving across the list. Try it on the numbers in &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039;, i.e. load the numbers column-based into a single list first, i.e. first all the numbers in column 1, then the numbers in column 2, and so forth in the list.&lt;br /&gt;
# Make a trend discoverer generator: &amp;#039;&amp;#039;&amp;#039;trend(List_of_numbers)&amp;#039;&amp;#039;&amp;#039;. It looks at a list of numbers in a moving window way and emits 1, if the next number is higher than the previous, and 0 otherwise. Any longer sequence of 0&amp;#039;s or 1&amp;#039;s in the generator output is a trend in the data. Check with &amp;#039;&amp;#039;ex1.dat&amp;#039;&amp;#039; (load same way a previous exercise) or another file of your choosing.&lt;br /&gt;
# Changing the previous exercise: Make a &amp;#039;&amp;#039;&amp;#039;find_trend(List_of_numbers, Minimum_trend_size)&amp;#039;&amp;#039;&amp;#039; generator, which return a tuple &amp;#039;&amp;#039;&amp;#039;(Position_Start, Size, Direction)&amp;#039;&amp;#039;&amp;#039; of where and how big the trends in List_of_numbers are. &amp;#039;&amp;#039;&amp;#039;Direction&amp;#039;&amp;#039;&amp;#039; is 0 or 1 as you want to know which direction the trend is going. &amp;#039;&amp;#039;&amp;#039;Position_start&amp;#039;&amp;#039;&amp;#039; is the position in the (zero-based) list, where the trend starts. &amp;#039;&amp;#039;&amp;#039;Size&amp;#039;&amp;#039;&amp;#039; is how long the trend of ascending/descending numbers is. This is surprisingly difficult. Test with a simple file of your own making to check your results.&lt;br /&gt;
# Make a generator &amp;#039;&amp;#039;&amp;#039;combinations()&amp;#039;&amp;#039;&amp;#039;, that takes a list of strings as input, e.g. &amp;#039;&amp;#039;&amp;#039;combinations([&amp;quot;GAVIL&amp;quot;, &amp;quot;ST&amp;quot;, &amp;quot;NQ&amp;quot;, &amp;quot;FWY&amp;quot;, &amp;quot;D&amp;quot;, &amp;quot;HKR&amp;quot;])&amp;#039;&amp;#039;&amp;#039;, and generates all possible combinations. A combination is formed by choosing 1 letter from the first string, 1 letter from the second string, and so forth, in that order, until a letter from all strings is chosen. The input list can have any number of strings and the strings can have any length (greater than 0). There must be NO REPEATS - random is not an acceptable library to use. As is obvious, the example has 5*2*2*3*1*3 = 180 different combinations, the first being GSNFDH. Print them all on the screen. If your input is [&amp;#039;0123456789&amp;#039;, &amp;#039;0123456789&amp;#039;, &amp;#039;0123456789&amp;#039;], then you will print the numbers from 000 to 999. Hint: A list of counters, 1 per string, could be useful in iterating through the combinations.&amp;lt;br&amp;gt;When can such a generator be useful? If you want to generate a list of antigens, which needs certain amino acids to be in certain positions.&lt;br /&gt;
&lt;br /&gt;
== Exercises for extra practice ==&lt;/div&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
</feed>