{"id":2389,"date":"2021-09-21T11:44:21","date_gmt":"2021-09-21T10:44:21","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/?p=2389"},"modified":"2021-09-21T11:44:21","modified_gmt":"2021-09-21T10:44:21","slug":"higher-sdd-reading-a-file-into-an-array","status":"publish","type":"post","link":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/2021\/09\/21\/higher-sdd-reading-a-file-into-an-array\/","title":{"rendered":"Higher &#8211; SDD &#8211; Reading a file into an array"},"content":{"rendered":"<p>The code below allows us to read a file into an array then process the data. In this case the processing is just to see how many of each of the numbers (1-6) there were in the file.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef getData(array):\r\n    txtFile=open(&quot;diceRolls.txt&quot;,&quot;r&quot;)\r\n    for x in range(len(array)):\r\n        array&#x5B;x]=int(txtFile.readline().strip())\r\n    \r\n    txtFile.close()\r\n    return array\r\n\r\ndef countOcc(array,target):\r\n    total=0\r\n    for number in array:\r\n        if number==target:\r\n            total=total+1\r\n            \r\n    return total\r\n\r\ndef main():\r\n    dice=&#x5B;0 for x in range(1000)]\r\n    dice=getData(dice)\r\n    for x in range(1,7):\r\n        print(&quot;Number of&quot;,x,&quot;'s=&quot;,countOcc(dice,x))\r\n        \r\nmain()\r\n\r\n<\/pre>\n<p>The text file is the result of 1000 D6 rolls.<br \/>\nCan you modify the program to work out which number has the largest number of occurrences?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The code below allows us to read a file into an array then process the data. In this case the processing is just to see how many of each of the numbers (1-6) there were in the file. def getData(array): txtFile=open(&quot;diceRolls.txt&quot;,&quot;r&quot;) for x in range(len(array)): array&#x5B;x]=int(txtFile.readline().strip()) txtFile.close() return array def countOcc(array,target): total=0 for number in &hellip;<\/p>\n","protected":false},"author":32,"featured_media":1386,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[432098,17009],"tags":[],"class_list":["post-2389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-higher","category-software-design-devlopment"],"jetpack_featured_media_url":"https:\/\/blogs.glowscotland.org.uk\/nl\/public\/ColtnessHS-ComputingScience\/uploads\/sites\/12638\/2016\/09\/Slide4.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/comments?post=2389"}],"version-history":[{"count":5,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2389\/revisions"}],"predecessor-version":[{"id":2394,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2389\/revisions\/2394"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/media\/1386"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/media?parent=2389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/categories?post=2389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/tags?post=2389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}