index.xhtml 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => 'Leaning Journal',
  21. 'subtitle' => 'CS 1101: Programming Fundamentals',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2 id="Unit1">Unit 1</h2>
  25. <p>
  26. First, I read through the learning guide, making a list of what I&apos;d need to read this week.
  27. One of the textbooks for this course is under the nonfree $a[GFDL].
  28. At some point, I&apos;ll write up an essay on exactly whey I consider this to be a nonfree license even when no invariant sections and no cover texts are used, but today, that essay is only partially complete and hasn&apos;t been released.
  29. The version of that textbook that can be linked to without the reader having a University of the People account is also behind a CloudFlare $a[CAPTCHA] wall, which is always aggravating.
  30. This isn&apos;t the school&apos;s fault, but CloudFlare really loves maliciously discriminating against us $a[Tor] users.
  31. Due to CloudFlare&apos;s high popularity, the CloudFlare $a[CAPTCHA] walls are common and get very old very quickly.
  32. The other textbook appears to not have a license at all, making it also nonfree.
  33. The textbooks are as follows:
  34. </p>
  35. <ul>
  36. <li>
  37. <a href="http://epe.lac-bac.gc.ca./003/008/099/003008-disclaimer.html?orig=/100/200/300/allan_publishing/history_personal_computer/index.html">Disclaimer - Electronic Collection</a>
  38. </li>
  39. <li>
  40. <a href="http://interactivepython.org/runestone/static/thinkcspy/index.html">Attention Required! | Cloudflare</a>
  41. </li>
  42. </ul>
  43. <p>
  44. In addition to some sections from the course textbooks, we also need to read these three pages:
  45. </p>
  46. <ul>
  47. <li>
  48. <a href="https://thocp.net/software/software_reference/introduction_to_software_history.htm">Introduction to Software History</a>
  49. </li>
  50. <li>
  51. <a href="http://computernostalgia.net./articles/HistoryofProgrammingLanguages.htm">History of Programming Languages, computer languages, software, applications</a>
  52. </li>
  53. <li>
  54. <a href="http://www.randomhistory.com./2008/06/26_software.html">A History of Software</a>
  55. </li>
  56. </ul>
  57. <p>
  58. One of the tasks for the week is to install Python on our machines.
  59. I&apos;ve already got that installed though, so that&apos;s one less task to worry about.
  60. I think that Python might come pre-installed on Debian systems.
  61. Even if it didn&apos;t though, installing it would have been super simple.
  62. Running <code>sudo aptitude install python</code> would have done the trick.
  63. </p>
  64. <p>
  65. I started by reading the non-textbook reading assignments first, taking notes in my <a href="https://y.st./en/coursework/CS1101/#Unit1">learning journal entry</a> as interesting facts came up.
  66. It seems that $a[PHP] (my native language) and Python (the language used in this course) were both invented at around the same time; 1994 and 1990, respectively.
  67. Recursive functions were first implemented in Algol.
  68. I would have thought that recursive functions would be definable in any language that supports named functions, but it seems that this isn&apos;t the case.
  69. Switch statements (known as &quot;case statements&quot; in Pascal) were first introduced in Pascal.
  70. C++ was ont of the first object-oriented programming languages.
  71. I&apos;m told that C++ is a mess of a language, and that developers should stick with C instead, but the concept of object-oriented programming is one that persists in a more clean form in other modern programing languages.
  72. As examples, modern $a[PHP] is partially object-oriented, while Python is completely object-oriented.
  73. The reading assignment says that Java is a textbook example of a good language, but I very much disagree.
  74. Java has a concept that it calls &quot;overloading&quot;.
  75. The premise is that multiple functions (methods) can share the same name, and the program decides which function is meant by a particular function call based on the types of the values passed in as arguments.
  76. This makes debugging a huge pain.
  77. For example, if you mistakingly pass the wrong variable into a method call or the variable that you pass in isn&apos;t the type that you think it is, the compiler often complains that the function that you tried to call doesn&apos;t exist.
  78. At that point, you try to figure out why your function definition is being ignored and/or try to find a typo in either the method name in the method call or the method name in the method definition.
  79. Having the ability to give multiple functions (methods) the same name is stupid, and even if you don&apos;t make use of that feature, it causes issues when trying to figure out why your program won&apos;t compile.
  80. It seems that C was the first portable language.
  81. That&apos;s kind of interesting, seeing as C is still in wide use today.
  82. Portability is no doubt vital, but it looks like improving upon this first portable language isn&apos;t an easy task; C is still a great language, even today, so it must have been fantastic for its time when first developed.
  83. The text also mentions &quot;$a[URL]s&quot;, but that term is a bit outdated.
  84. The correct term these days is &quot;$a[URI]s&quot;.
  85. </p>
  86. <p>
  87. It took me time to get back to my coursework because life is pretty hectic for me right now, but I eventually got back on task and read the assigned sections from the text book.
  88. I misread something near the beginning of the book: I thought that it was calling C++ a low-level language, but saying that programs written in low-level languages cannot be made to run on multiple types of machines without rewriting them for each type of machine.
  89. I happen to know of a game that I used to play though, a cross-platform game written in C++.
  90. The book actually was calling C and C++ high-level languages though, so it&apos;s all good.
  91. I consider C and C++ to be low-level languages myself, but the thing is, high to low is a range.
  92. C is a higher-level language than, say, assembly code, but it&apos;s lower than $a[PHP] and Python.
  93. The book does make a mistake in regards to interpreted languages though.
  94. It says that the interpreter processes the script a little at a time, alternating between reading the script and performing actions.
  95. This isn&apos;t always true.
  96. $a[PHP], for example, reads and interprets the <strong>*entire script*</strong> before performing even a <strong>*single*</strong> action.
  97. The $a[PHP] interpretor &quot;compiles&quot; all of the code in the beginning, but there&apos;s no compiled executable, making it an interpreted language, not a compiled language.
  98. Because of this, if there is a syntax error anywhere in the script, the script will fail to execute before anything happens.
  99. It&apos;s important to note the difference between syntax errors and runtime errors though, as a script with runtime errors can and will function up to the point in which the error occurs.
  100. Bash, on the other hand, does as the book describes.
  101. It reads the script line by line, interpreting each line as a command and executing it before moving on to the next line.
  102. I&apos;m not sure what Python does.
  103. While I&apos;m fluent in Python, it&apos;s not a language that I use often enough to remember the details of how syntax errors are treated.
  104. The book also says that Linus Torvalds invented Linux, the operating system, and that this operating system evolved out of a simple program that they wrote.
  105. This is untrue.
  106. The Linux operating system evolved out of the $a[GNU] project&apos;s operating system.
  107. The Linux operating system contains the Linux kernel, and it&apos;s the Linux kernel that Torvalds wrote and that evolved from Torvalds&apos; simple program.
  108. The part of the reading that mentions that people treat computers as people is kind of funny.
  109. I consider my Debian machine to be sort of like a good friend.
  110. I&apos;m often lost without it, and it almost never fails me.
  111. One of my parents though constantly complains about their computer.
  112. They think that all computers are stupid, just because they&apos;re running an operating system that&apos;s known to be a major pain to work with.
  113. They refuse to upgrade to something better though, something that would help them see their computer as an comrade as well.
  114. </p>
  115. <p>
  116. Next, I wrote up my initial discussion post.
  117. With how late my initial post was, I was very lucky that someone responded to it at all.
  118. To be honest, I was worried.
  119. The coming week should be less hectic than this week, so I should be able to get my discussion post in sooner next time.
  120. It seems that I made one fellow classmate think that I&apos;m involved with a free software project.
  121. Perhaps the wording of my post was bad.
  122. Technically, I am the sole developer of the <a href="https://notabug.org./y.st./include.d/releases">include.d</a> library and some lesser projects that depend on it, but I hardly ever have time to work on them these days lately.
  123. Furthermore, these projects are important to no one but myself.
  124. I&apos;m not some highly-known developer making a difference in the world, I&apos;m just a simple computer user that avoids using proprietary software and media.
  125. The other person that responded to me bought up the great example of office suites.
  126. Should they be written in a compiled language or an interpreted language?
  127. It seems like both have advantages and trade-offs.
  128. In fact, LibreOffice is written in C++, a compiled language, as well as Python, an interpreted language.
  129. It also contains Java components.
  130. Java&apos;s strange.
  131. You can&apos;t call it a compiled language, because you can&apos;t compile Java into something that will run without an interpreter, but at the same time, you can&apos;t call it an scripted (interpreted) language, because it can&apos;t run uncompiled scripts.
  132. You must compile the source code into Java byte code, then run it in the Java interpreter.
  133. From one the students that I replied to, I learned something that I didn&apos;t know before.
  134. Cobol can&apos;t be used to write system software, only applications.
  135. This means that Cobol is either not a complete language or it&apos;s a domain-specific language.
  136. I assume that if it can&apos;t be used to create system software, it&apos;s because of some limitation such as an inability to create software without a $a[GUI].
  137. In any case, it&apos;s not useful as a general-purpose language and shouldn&apos;t be used as such.
  138. Still, I find this surprising.
  139. It&apos;s true that I&apos;ve never tried to learn Cobol, but I hear it mentioned on a semi-regular basis.
  140. I thought that it was one of the more popular of the older languages still in use (not super-popular like C, but not really a fringe language either), which would imply that it&apos;s a useful language.
  141. I don&apos;t have time this week, but I&apos;ll have to look into Cobol more in the future to find out how it works.
  142. </p>
  143. <p>
  144. Finally, with everything else completed, I took the ungraded quiz.
  145. At first, I was a bit annoyed that we&apos;re being quizzed on history instead of learning actual programming.
  146. Which programming language wasn&apos;t developed in the 1950s?
  147. In what school was C developed?
  148. That information is useless.
  149. But then came the question about the origin of the term &quot;bug&quot;.
  150. I&apos;m almost certain that that wasn&apos;t even discussed in the reading material.
  151. In fact, as I&apos;ve heard the story before, I was quite surprised that it wasn&apos;t!
  152. If we&apos;re going to be quizzed on things not covered by the text, how do we stand a chance of success?
  153. We have no way to know what random facts we&apos;re going to need to know for the quizzes, so research on our own is of no use against this problem, as we have no way to know what to research.
  154. I really hope that I just missed the part about the moth, because then it would at least be my own fault, not something unavoidable.
  155. The history components of this course are going to be a major challenge for me.
  156. I learned to program before taking any programming courses, and since then, have taken a few.
  157. Because of that, I don&apos;t think that the code work in this introductory course will be a problem.
  158. It doesn&apos;t hurt either that the programming to be done in this course is to be done in Python.
  159. Python isn&apos;t my favorite language or anything, but there&apos;s no denying that it&apos;s one of the easier languages to work with.
  160. I&apos;m bad at history though.
  161. I&apos;m bad at remembering useless trivia, such as names and dates.
  162. </p>
  163. <p>
  164. I&apos;m not sure how to answer the posed question of what skills I&apos;m gaining so far.
  165. We haven&apos;t even begun to touch code yet!
  166. At the moment, we&apos;re still covering the basics.
  167. I suppose that I am learning to think more deeply about what language to use when.
  168. Compiled and scripted languages both have their places, a fact that I already knew, but the line between when each is better can be a bit blurry.
  169. As for what I&apos;m learning about myself as a learner, I&apos;m not sure that I&apos;ve picked something new about that up from the course yet, as like I&apos;ve said, we&apos;ve not really begun.
  170. However, I have found because of my hectic schedule right now that I don&apos;t really do well in my studies when my time and energy are stretched to the max.
  171. This week has been horridly stressful, and I feel like it&apos;s taken a toll on my coursework.
  172. In my other course, I ended up having to write most of an <a href="https://y.st./en/coursework/PHIL1404/Apple_and_ethics.xhtml">essay</a> in a single day because I hadn&apos;t had the time to get to it until then.
  173. I hit about four times the minimum word count, as I always put in a good effort, but I can&apos;t help but feel like I would have been able to polish the essay more had I had more free time and more free energy.
  174. In this course, I had to submit my main discussion post with only just over a day left on the clock, which was frustrating.
  175. Again, I would have loved to have had the time and energy to put more into that than I did.
  176. My most important thought in closing is probably that I hope next week will go more smoothly and that I wish I had had the time to do better on my assignments this week, including this one.
  177. </p>
  178. <h3>Exercises:</h3>
  179. <h4>Exercise 1.1</h4>
  180. <p>
  181. I am &lt;=&gt; CS 1101 online at [+].
  182. <em>(I tried to get fancy and use characters from outside the English language in that sentence, but the University of the People submission form choked on those characters.)</em>
  183. </p>
  184. <p>
  185. I University of the People taking am CS 1101 at online.
  186. </p>
  187. <h4>Exercise 1.3</h4>
  188. <blockquote>
  189. <pre>yst@newdawn.hn.y.st.:~\$ <code>python</code>
  190. Python 2.7.9 (default, Jun 29 2016, 13:08:31)
  191. [GCC 4.9.2] on linux2
  192. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  193. &gt;&gt;&gt; <code>help()</code>
  194. Welcome to Python 2.7! This is the online help utility.
  195. If this is your first time using Python, you should definitely check out
  196. the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.
  197. Enter the name of any module, keyword, or topic to get help on writing
  198. Python programs and using Python modules. To quit this help utility and
  199. return to the interpreter, just type &quot;quit&quot;.
  200. To get a list of available modules, keywords, or topics, type &quot;modules&quot;,
  201. &quot;keywords&quot;, or &quot;topics&quot;. Each module also comes with a one-line summary
  202. of what it does; to list the modules whose summaries contain a given word
  203. such as &quot;spam&quot;, type &quot;modules spam&quot;.
  204. help&gt; <code>quit</code>
  205. You are now leaving help and returning to the Python interpreter.
  206. If you want to ask for help on a particular object directly from the
  207. interpreter, you can type &quot;help(object)&quot;. Executing &quot;help(&apos;string&apos;)&quot;
  208. has the same effect as typing a particular string at the help&gt; prompt.
  209. &gt;&gt;&gt; <code>help(print)</code>
  210. File &quot;&lt;stdin&gt;&quot;, line 1
  211. help(print)
  212. ^
  213. SyntaxError: invalid syntax
  214. &gt;&gt;&gt; <code>help(&apos;print&apos;)</code>
  215. &gt;&gt;&gt; <code>quit</code>
  216. Use quit() or Ctrl-D (i.e. EOF) to exit
  217. &gt;&gt;&gt; <code>quit()</code>
  218. yst@newdawn.hn.y.st.:~\$ </pre>
  219. </blockquote>
  220. <h4>Exercise 1.4</h4>
  221. <blockquote>
  222. <pre>yst@newdawn.hn.y.st.:~\$ <code>python</code>
  223. Python 2.7.9 (default, Jun 29 2016, 13:08:31)
  224. [GCC 4.9.2] on linux2
  225. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  226. &gt;&gt;&gt; print <code>4.35/1.61</code>
  227. 2.70186335404
  228. &gt;&gt;&gt; <code>quit()</code>
  229. yst@newdawn.hn.y.st.:~\$ </pre>
  230. </blockquote>
  231. <h3>Discussion board post drafts</h3>
  232. <p>
  233. I&apos;m not sure why, but the learning journal assignment instructions this week say to draft the discussion posts here in the learning journal.
  234. So, these were the posts I made.
  235. Typically, I would have drafted them in my main journal, instead of my learning journal.
  236. Either option allows me to compose my entries in $a[XHTML] though, which gives me much more control over the formatting while at the same time allowing me to ignore irrelevant details such as text size and font.
  237. </p>
  238. <blockquote>
  239. <p>
  240. Comparing all of the advantages of a specific compiled language and a specific interpreted language doesn&apos;t seem to present any useful information about the differenced between compiled and interpreted languages.
  241. For example, comparing the C programming language to Python shows us that Python is easier to work with.
  242. Python is a higher-level language than C, so Python takes care of all the little things automatically that C requires that you do manually.
  243. That said, C has the advantage of being more efficient in terms of resource usage, both because it&apos;s compiled and because it&apos;s a lower-level language.
  244. In both cases, the differences are because of the level of the language, not how whether the language is compiled or interpreted.
  245. </p>
  246. <p>
  247. It might be more useful to discuss compiled languages in general versus interpreted languages in general.
  248. Compiled languages are more efficient than interpreted languages because the machine only has to compile the code once.
  249. On the inside, the interpreter is basically compiling the script <strong>*every time that you run it*</strong>.
  250. That&apos;s a lot of extra compiling, if you run the script often!
  251. On the other hand, interpreted languages offer the flexibility of easier modification.
  252. If you&apos;re a part of the free software world like I am, having the source code and the executable be the same thing just makes sense.
  253. There&apos;s a problem though: interpreted languages require, well, an interpreter.
  254. This interpretor could be written in an interpreted language itself, but then you&apos;d need an interpretor for the interpretor!
  255. It&apos;s turtles all the way down, if you try to do it that way.
  256. Instead, the interpretor itself is a great example of a program that should be written in a compiled language, so it can be built into a binary executable that the computer can figure out how to use without needing another interpretor.
  257. Anything that requires a high level of efficiency, such as a kernel or other well-used system component, should also be written in a compiled language.
  258. Interpreted languages are good for almost everything else.
  259. The more simple and less vital the software, the more you should consider using an interpreted language.
  260. Are you trying to write code to automate a task?
  261. Use an interpreted language.
  262. Are you writing a game?
  263. An interpreted language is a good idea.
  264. Are you writing code for your website?
  265. Unless the website sees <strong>*very*</strong> heavy traffic (think Wikipedia), an interpreted language is probably your best bet.
  266. Anything that you&apos;re going to have to modify frequently should also be written in an interpreted language.
  267. </p>
  268. </blockquote>
  269. <blockquote>
  270. <p>
  271. When I said that I&apos;m a part of the free software world, I didn&apos;t mean that I&apos;m involved with a specific project.
  272. I&apos;ve written a couple bug patches for the game Minetest, but other than that, I&apos;m a free software <strong>*user*</strong>.
  273. I don&apos;t run any proprietary software whatsoever.
  274. I also only buy free music, music under a {$a['CC BY']} or {$a['CC BY-SA']} license.
  275. </p>
  276. <p>
  277. I am working on a library of classes, functions, and constants for $a[PHP] called <a href="https://notabug.org./y.st./include.d/releases">include.d</a>, but that&apos;s mainly for my personal uses and likely isn&apos;t valuable to others.
  278. It&apos;s still under the $a[GNU] $[GPL], but I haven&apos;t had time as of late to work on it much because life has been hectic.
  279. I&apos;m in the middle of a move to a new house (carrying all my stuff on foot, I don&apos;t drive), school, and work ...
  280. My code likely won&apos;t be updated for quite a while.
  281. My current goal with include.d though is to add scheme-specific $a[URI] parsers so I can get back to building my <a href="https://notabug.org./y.st./OnionSpider">darknet spider</a> that needs them.
  282. The spider is almost certainly in a broken state right now though because of updates to include.d, so I don&apos;t recommend trying to run it until I finish updating include.d again and rework the spider to use the new version.
  283. I also release my <a href="https://y.st./">website</a> and all its content under the $a[GNU] $a[GPL].
  284. </p>
  285. </blockquote>
  286. <blockquote>
  287. <p>
  288. I can&apos;t say for sure about if the Microsoft Office developers could make Microsoft Office work well in an interpreted language, as I&apos;m not convinced that it works well now.
  289. It&apos;s a bloated office suite in need of a serious tune-up and I don&apos;t think that the developers have what it takes to fix that, no matter the language.
  290. In general though, I think that other office suites could be written to run well in an interpreted language.
  291. Office suites are more complex than other pieces of software (needlessly complex, at least in the case of word processing), so it might be better to write those in a compiled language after all.
  292. LibreOffice, for example, is written in a combination of C++, Java, and Python.
  293. I don&apos;t claim to know what parts are written in what language, but I imagine that the base components that need to run quickly are written in C++ while the less vital components, such as the user interface and some of the more complex functionality, are written in Java and Python.
  294. Depending on the game, you might be better off in a compiled or interpreted language, even.
  295. Simpler games such as two-dimensional platformers would be better off in an in interpreted language, while resource-intensive games would be better off in a compiled language.
  296. In part, it&apos;s a trade-off between making the code easier to write and maintain or making the code easier for the machine to handle.
  297. The more complex the task, the more machine-optimization will need to happen to keep the software from taking too many of the machine&apos;s resources.
  298. When the task is simpler though, there&apos;s lots of wiggle room, so you can put more of the burden on the machine and less on the human.
  299. </p>
  300. </blockquote>
  301. <blockquote>
  302. <p>
  303. C is a great language to compare to Python.
  304. Not only is C pretty much the de facto main compiled language, it&apos;s also the language than many interpreters (and I think compilers) are written in!
  305. The Python interpreter is one such C-based interpreter.
  306. Even when you&apos;re not using C directly, you&apos;re still probably using C.
  307. </p>
  308. <p>
  309. I think that C&apos;s strength, relying on libraries to do the machine-specific lifting and having a platform-independent interface is one of the major strengths of Python as well.
  310. Instead of a machine-specific library though, Python has a machine-specific version of the interpretor, compiled from the same source code.
  311. Just like with C, the code can be written once and still run on multiple processors and multiple systems.
  312. </p>
  313. </blockquote>
  314. <blockquote>
  315. <p>
  316. You say that Cobol cannot be used to write system software; it can only be used for writing application software.
  317. Would you consider this weakness to be particularly detrimental?
  318. It means that Cobol can&apos;t handle certain functionalities that one might need to write into their code.
  319. At the same time though, removing unneeded features sometimes makes the language more efficient for its intended use.
  320. Would you consider Cobol to simply be a domain-specific language, good at what it&apos;s designed to do, or a handicapped language, unable to perform certain simple tasks?
  321. </p>
  322. </blockquote>
  323. <blockquote>
  324. <p>
  325. I&apos;d also argue that debugging Java is a pain in the neck to do.
  326. That disadvantage alone is the reason that I don&apos;t write much in Java.
  327. If I can ever get my Android development environment to function though, I might suck it up and start using Java for mobile application development.
  328. There are several applications that I&apos;d love to write, as I don&apos;t see the applications that I need available elsewhere yet.
  329. </p>
  330. <p>
  331. As for C#, I agree with you that it offers little portability.
  332. Attempts are being made to make C# code run on non-Windows platforms, but I&apos;m told that they&apos;re incomplete at best.
  333. I don&apos;t recommend that anyone use C# for writing software, as it binds your code to only work on one platform; and not even a very good platform at that.
  334. </p>
  335. <p>
  336. How would you compare these two languages to Python?
  337. </p>
  338. </blockquote>
  339. <h2 id="Unit2">Unit 2</h2>
  340. <p>
  341. I began the week by looking over the assignments as I often do.
  342. First off, I checked the <a href="https://y.st./en/coursework/CS1101/#Unit2">learning journal</a> assignment so that I&apos;d know if it was anything like <a href="https://y.st./en/coursework/CS1101/#Unit1">last week&apos;s</a>.
  343. That would tell me not only where I should take my notes for the week, but also the level of detail my notes should have.
  344. When not taking notes on my activities in my learning journal, I instead take them in my main journal.
  345. Sure enough, the learning journal assignment for this week is the same as last week&apos;s.
  346. Next, I checked the programming assignment for the week, the first in the course.
  347. Strangely, it specifies that a Microsoft Word document must be handed in.
  348. In general, University of the People accepts both the Microsoft Office formats and the OpenOffice.org/LibreOffice formats.
  349. It doesn&apos;t bother me at all, as LibreOffice makes the conversion simple, but it does make this assignment seem ... out of place.
  350. Additionally, word processing documents of any format seem like a bad fit for this assignment.
  351. The options are to take a screenshot of the output or to copy and paste the text of the output.
  352. In case of screenshots, word-processing documents are a horrid fit.
  353. A bare image file would be easier to work with and easier to read on the receiving end.
  354. In case of pasted text, a word-processing document would be less bad, but a plain text file would still be clearly a better option.
  355. The assignment was too easy to put off though, so I set to work on that right away.
  356. </p>
  357. <p>
  358. First, I learned that the Python prompt is not $a[IDLE].
  359. The assignment specifically requests that I use $a[IDLE].
  360. I was supposed to install Python and $a[IDLE] last week, but I misunderstood and thought that $a[IDLE] was a part of Python.
  361. It isn&apos;t.
  362. Running <code>sudo aptitude install idle</code> on the command line rectified the issue though.
  363. </p>
  364. <p>
  365. $a[IDLE]&apos;s combination of being a shell and being a text editor is strange.
  366. It also has an annoying habit of adding sequences of space characters instead of singular tab characters when I hit the tab key.
  367. There doesn&apos;t seem to be any way to override this obnoxious behavior either, though the number of spaces that $a[IDLE] uses can be configured.
  368. In an attempt to work around the problem, I kept a tab character on my clipboard and pasted it as needed, but it&apos;s an ugly solution and I would have expected better from $a[IDLE].
  369. That said, we didn&apos;t actually need any indention in this assignment, so that was a small, wasted effort.
  370. Additionally, there doesn&apos;t seem to be an option to visualize whitespace.
  371. Again, I expect more out of my text editors.
  372. As for my actual <a href="https://y.st./en/coursework/CS1101/t_area.py.xhtml">script</a>, it contains more comments than code.
  373. </p>
  374. <p>
  375. The reading assignment for the week is a few chapters from our assigned textbooks in addition to a couple of videos:
  376. </p>
  377. <ul>
  378. <li>
  379. <a href="https://www.youtube.com./watch?v=husPzLE6sZc">Introduction to Programs Data Types and Variables - YouTube</a>
  380. </li>
  381. <li>
  382. <a href="https://www.youtube.com./watch?v=iZAtkS0F-Zo">Fun with Strings - YouTube</a>
  383. </li>
  384. </ul>
  385. <p>
  386. The first video reminded me of Python&apos;s obnoxious habit of adding a new line character after everything that you tell it to <code>print</code>.
  387. For new users, this might be helpful.
  388. For experienced users, this makes things more difficult.
  389. We know to add <code>\\n</code> when we need a new line.
  390. So what do we do when we <strong>*don&apos;t*</strong> need a new line in Python?
  391. Well, there&apos;s the option of adding a comma after the thing to print, which prevents Python from outputting a new line, but it causes it to output a space instead.
  392. What if we need Python to output only and exactly what we tell it to?
  393. Well, the short answer is that it&apos;s not possible.
  394. The longer answer is that in most cases, you can use concatenation to achieve your desired result, but you still can&apos;t get Python to output only and exactly what you tell it to.
  395. </p>
  396. <p>
  397. The book says that <code>//</code> is used for floor division instead of <code>/</code>.
  398. The book must be working with Python3 then, not Python2.
  399. After seeing that, I started using Python3 for the exercises instead of my system&apos;s default, Python 2.
  400. In the next Debian release, Python3 will likely be the default.
  401. </p>
  402. <p>
  403. With everything done for the week, I looked over my code assignment one last time, then submitted it.
  404. I considered adding <code>#!/usr/bin/python3</code> to the top of the script, but I don&apos;t know who will be grading it.
  405. If they&apos;re not on the same operating system as I am, which is highly likely, they won&apos;t have Python installed in the same place.
  406. That line might make the script fail to execute if Python is instead installed elsewhere.
  407. I tried to take the ungraded quiz, but I was too late.
  408. I thought that the ungraded quizzes stayed open until the end of the term, but I either remembered incorrectly, or the quizzes are set up differently this term than last.
  409. It&apos;s been a hectic week due to non-university events, such the move that I&apos;m in the middle of, so I thought that I&apos;d take a little bit of a break before taking the quiz.
  410. Bad idea.
  411. If I&apos;m to get a chance to test my knowledge with the quizzes, I have to do it right away.
  412. </p>
  413. <p>
  414. My final thought for the week is that I must push forward.
  415. There&apos;s no time for rest until everything, both graded and ungraded, is done.
  416. </p>
  417. <h3>Exercises:</h3>
  418. <h4>Exercise 2.1</h4>
  419. <blockquote>
  420. <pre>yst@newdawn.hn.y.st.:~$ <code>python</code>
  421. Python 2.7.9 (default, Jun 29 2016, 13:08:31)
  422. [GCC 4.9.2] on linux2
  423. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  424. &gt;&gt;&gt; <code>m = 1,000,000</code>
  425. &gt;&gt;&gt; <code>type(m)</code>
  426. &lt;type &apos;tuple&apos;&gt;
  427. &gt;&gt;&gt; <code>exit()</code>
  428. yst@newdawn.hn.y.st.:~$ </pre>
  429. </blockquote>
  430. <h4>Exercise 2.2</h4>
  431. <blockquote>
  432. <pre>yst@newdawn.hn.y.st.:~$ <code>python3</code>
  433. Python 3.4.2 (default, Oct 8 2014, 10:45:20)
  434. [GCC 4.9.1] on linux
  435. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  436. &gt;&gt;&gt; <code>5</code>
  437. 5
  438. &gt;&gt;&gt; x = <code>5</code>
  439. &gt;&gt;&gt; x + <code>1</code>
  440. 6
  441. &gt;&gt;&gt; <code>exit()</code>
  442. yst@newdawn.hn.y.st.:~$ </pre>
  443. </blockquote>
  444. <h3>Exercise 2.3</h3>
  445. <blockquote>
  446. <pre>yst@newdawn.hn.y.st.:~$ <code>python3</code>
  447. Python 3.4.2 (default, Oct 8 2014, 10:45:20)
  448. [GCC 4.9.1] on linux
  449. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  450. &gt;&gt;&gt; <code>width = 17</code>
  451. &gt;&gt;&gt; <code>height = 12.0</code>
  452. &gt;&gt;&gt; <code>delimiter = &apos;.&apos;</code>
  453. &gt;&gt;&gt; <code>p0 = width/2</code>
  454. &gt;&gt;&gt; <code>print(p0, type(p0))</code>
  455. 8.5 &lt;class &apos;float&apos;&gt;
  456. &gt;&gt;&gt; <code>p1 = width/2.0</code>
  457. &gt;&gt;&gt; <code>print(p1, type(p1))</code>
  458. 8.5 &lt;class &apos;float&apos;&gt;
  459. &gt;&gt;&gt; <code>p2 = height/3</code>
  460. &gt;&gt;&gt; <code>print(p2, type(p2))</code>
  461. 4.0 &lt;class &apos;float&apos;&gt;
  462. &gt;&gt;&gt; <code>p3 = 1 + 2 * 5</code>
  463. &gt;&gt;&gt; <code>print(p3, type(p3))</code>
  464. 11 &lt;class &apos;int&apos;&gt;
  465. &gt;&gt;&gt; <code>p4 = delimiter * 5</code>
  466. &gt;&gt;&gt; <code>print(p4, type(p4))</code>
  467. ..... &lt;class &apos;str&apos;&gt;
  468. &gt;&gt;&gt; <code>exit()</code>
  469. yst@newdawn.hn.y.st.:~$</pre>
  470. </blockquote>
  471. <h4>Exercise 2.4</h4>
  472. <blockquote>
  473. <pre>yst@newdawn.hn.y.st.:~$ <code>python3</code>
  474. Python 3.4.2 (default, Oct 8 2014, 10:45:20)
  475. [GCC 4.9.1] on linux
  476. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  477. &gt;&gt;&gt; <code>pi = 3.1415926535897931</code>
  478. &gt;&gt;&gt; <code>print((4/3) * pi * 5**3)</code>
  479. 523.5987755982989
  480. &gt;&gt;&gt; <code>print((24.95 * 0.6 + 0.75) * 60 + 2.25)</code>
  481. 945.4499999999999
  482. &gt;&gt;&gt; <code>print(6.87 + 2 * 8.25 + 3 * 7.2)</code>
  483. 44.97
  484. &gt;&gt;&gt; <code>exit()</code>
  485. yst@newdawn.hn.y.st.:~$</pre>
  486. </blockquote>
  487. <h2 id="Unit3">Unit 3</h2>
  488. <p>
  489. One of the first things that I saw this week was a link at the bottom of the classroom index page that read:
  490. </p>
  491. <blockquote>
  492. <p>
  493. Dia - Download Windows version of Dia diagram tool for flowcharts
  494. </p>
  495. </blockquote>
  496. <p>
  497. Admittedly, I panicked a bit.
  498. Did we need this windows-specific software to complete this week&apos;s assignment?
  499. Would I be able to complete this week&apos;s assignment, or would I have to automatically fail by not being able to use the software?
  500. Thankfully, the link was just poorly-phrased and falsely assumed that all students are running Windows.
  501. The tool is also available for other systems and is even <a href="apt:dia">available in the Debian repository</a>.
  502. Running <code>sudo aptitude install dia</code> will install it without any hassle.
  503. </p>
  504. <p>
  505. Unfortunately, due to life circumstances, I wasn&apos;t able to get right on my coursework as I&apos;d like to have.
  506. I keep thinking that each next week will be better, but so far, it hasn&apos;t been that way.
  507. Lack of time has been my biggest challenge for the week, as I continue to move my stuff from my old house to my new apartment on foot (a half-hour walk when unburdened), go to work, attend school, try to clean up my old house for sale, debug my malfunctioning mobile device, and more.
  508. </p>
  509. <p>
  510. In addition to a few chapters of the textbook, the reading list for the week was as follows:
  511. </p>
  512. <ul>
  513. <li>
  514. <a href="https://users.evtek.fi/~jaanah/IntroC/DBeech/3gl_flow.htm">Designing programs with flow charts</a>
  515. </li>
  516. <li>
  517. <a href="https://users.evtek.fi/~jaanah/IntroC/DBeech/3gl_pseudo.htm">3GL Program Design - Pseudocode</a>
  518. </li>
  519. <li>
  520. <a href="https://www.youtube.com./watch?v=DRwtJ1WjzI8">Flowcharting - Part 1 - YouTube</a>
  521. </li>
  522. <li>
  523. <a href="https://www.youtube.com./watch?v=Rg-fO7rDsds">What Is Pseudocode? - YouTube</a>
  524. </li>
  525. <li>
  526. <a href="https://www.youtube.com./watch?v=UNWF6eBErWs">Flowcharting - Part 2 - YouTube</a>
  527. </li>
  528. <li>
  529. <a href="https://www.youtube.com./watch?v=hN9xemJYwos">Problem Solving Techniques #8: Flow Charts - YouTube</a>
  530. </li>
  531. <li>
  532. <a href="http://faculty.ccri.edu./mkelly/COMI1150/PseudocodeBasics.pdf">OBJECTIVES: - PseudocodeBasics.pdf</a>
  533. </li>
  534. <li>
  535. <a href="http://web.cerritos.edu/jwilson/SitePages/cpp_language_resources/gaddis_sowc++7e_pdf_appendices_/App_D_Intro_to_Flowcharting.pdf">Z04_GADD6253_07_SE_APP4 - App_D_Intro_to_Flowcharting.pdf</a>
  536. </li>
  537. </ul>
  538. <p>
  539. There was one more video, but it requires Adobe Flash to watch.
  540. As a result, I was unable to watch that video.
  541. </p>
  542. <p>
  543. One of the great things about pseudocode is that once you&apos;ve used it a while, you start to have half-functional programs after writing out your pseudocode mock-ups.
  544. By its very nature, pseudocode can express any algorithm that could be spelled out to a computer, and likely, most if not all algorithms that a computer can&apos;t handle.
  545. Flowcharts suffer from severe limitations though due to their two-dimensional and graphical nature.
  546. Simple algorithms can be expressed well as flowcharts, and more complex algorithms can be expressed using multi-part flowcharts, but pseudocode has at least as much expressive power as, if not more than, flowcharts.
  547. I would love to use pseudocode for the project this week, but I&apos;ve been programming for too long.
  548. My pseudocode looks like actual code, and might actually run if plugged into $a[PHP] instead of Python (I think in $a[PHP] because I use it daily, I don&apos;t use Python often).
  549. I fear that as it&apos;s graded by other students, they might think that I missed the point of pseudocode and didn&apos;t do the assignment correctly.
  550. It&apos;s hard to misinterpret flowcharts as functional code though, so that&apos;s the path that I chose for completing the assignment for the week.
  551. (Intentionally writing pseudocode that doesn&apos;t look like code would be possible, but it would be a step backwards.)
  552. Additionally, using a flowchart instead of pseudocode gave me a chance to work with Dia, which I&apos;d never even heard of before this week.
  553. </p>
  554. <p>
  555. I probably should have put in an effort to build my <a href="https://y.st./en/coursework/CS1101/calculator_flowchart.xhtml">flowchart</a> in $a[XHTML] and $a[CSS], for a better file size and easier archival, but I didn&apos;t think of that until I&apos;d finished building it in Dia.
  556. Besides, it gave me a chance to figure out the basics of Dia.
  557. I&apos;d say that the main thing that I learned this week was how to set up a basic flowchart using Dia, though I wasn&apos;t able to perfect my use of it by any means.
  558. My Dia project looked the way that I wanted it to before exporting as a $a[PNG], but strangely, the exported image doesn&apos;t look quite like the project.
  559. The main discrepancy is in the largest of the error messages that I put in the flowchart.
  560. In my project, the parallelogram around the text is large enough that the full text is displayed.
  561. However, in the exported image, the opening single quote is too close to the edge of the shape and the ending single quote overlaps with the border.
  562. Because of this, it looks like there&apos;s no ending quote at all.
  563. </p>
  564. <p>
  565. One of the assigned pages for reading says that key words should be in all capital letters.
  566. This is not a hard requirement, and in fact, I don&apos;t recommend doing it.
  567. All that it serves to do is make your pseudocode ugly.
  568. This isn&apos;t problematic in the beginning, I guess, but it becomes a bigger issue as your pseudocode begins to look more like real code.
  569. (When I say &quot;hard requirement&quot;, I mean an absolute requirement.
  570. The opposite would be a soft requirement, not an easy requirement.)
  571. That same page brings up the need for an ending key word in addition to a beginning key word.
  572. For example, ending an <code>if</code> block needs an <code>endif</code> key word.
  573. Again, this isn&apos;t a hard requirement because of the indention, but it&apos;s a good idea.
  574. Another option is to use curly braces or the like, but I personally find the key words such as <code>endif</code> to be more readable.
  575. </p>
  576. <p>
  577. Once more, I&apos;ve had to submit my discussion response at the last minute, and once more, I&apos;m lucky that anyone had time to respond to it.
  578. I can&apos;t wait until my move is complete and I have more time and energy to get my coursework completed earlier.
  579. I didn&apos;t have any particularly noteworthy interactions this week, likely because of time constraints, but I did respond to one student that had an idea that made me wonder.
  580. They mentioned using pseudocode in documentation.
  581. How would one do this?
  582. How would one document code with examples without the examples consisting of real code?
  583. And what purpose would this serve?
  584. </p>
  585. <p>
  586. One of the six programming assignments that I graded had some confusing parts, but did meet all the requirements.
  587. First, it included this comment in the upper comments section:
  588. </p>
  589. <blockquote>
  590. <pre><code># Licence: &lt;your licence&gt;</code></pre>
  591. </blockquote>
  592. <p>
  593. So ... what license is it covered by then?
  594. Additionally, it had this code fragment at the top of the actual code:
  595. </p>
  596. <blockquote>
  597. <pre><code>def main():
  598. pass
  599. if __name__ == &apos;__main__&apos;:
  600. main()</code></pre>
  601. </blockquote>
  602. <p>
  603. It defines a function that does nothing, then calls it if the script is the main one being run, instead of being run as a module.
  604. What is even the point?
  605. Technically though, this doesn&apos;t mess with the output of their script, it just has the computer put in a little extra effort for no gain.
  606. Another submission not only provides actual units in the output, but also shows the same area converted into metric units!
  607. I should have included the units too, at a bare minimum, but I was too focused on trying to get exactly the expected output, as evidenced by the comments section of <a href="https://y.st./en/coursework/CS1101/t_area.py.xhtml">my code</a>.
  608. Conversion to metric units would have been a nice touch too.
  609. I find it a bit interesting that you can tell a bit about the operating system of the user based on their code.
  610. My text editor, <a href="apt:geany">Geany</a>, displays &quot;invisible&quot; characters for me.
  611. Because of that, I can see the line terminators.
  612. On Linux, $a[BSD], and OS X systems, the lines end in a line feed.
  613. On pre-OS X Macs, a carriage return is used instead.
  614. (I saw no submissions that fit that description though.)
  615. In submissions that came from Windows users, I saw both a carriage return and a line feed, because I guess that Microsoft likes line terminators to take twice as much space on disk.
  616. </p>
  617. <p>
  618. When taking the ungraded quiz, it was asked whether this statement was true or false:
  619. </p>
  620. <blockquote>
  621. <p>
  622. One of the advantages of flowcharts is that it is hard to modify.
  623. </p>
  624. </blockquote>
  625. <p>
  626. How would that possibly be an advantage?
  627. I mean, it&apos;s true that they&apos;re harder to modify than pseudocode, but that&apos;s a <strong>*disadvantage*</strong>.
  628. </p>
  629. <p>
  630. Thankfully, the graded quiz wasn&apos;t a pure history quiz.
  631. Most of the historical information, such as at what university each programming language was invented in, is useless.
  632. I have trouble retaining information that serves no value.
  633. Some history questions were asked though, so I did lose some points on those.
  634. One frustratingly-ambiguous programming question was present too:
  635. </p>
  636. <blockquote>
  637. <p>
  638. What output will the following python command produce:
  639. </p>
  640. <p>
  641. &gt;&gt;&gt; percentage = ( 60 * 100) / 55<br/>
  642. &gt;&gt;&gt; print (percentage)
  643. </p>
  644. </blockquote>
  645. <p>
  646. It depends.
  647. Are we talking about Python2 or Python3?
  648. At first, I answered according to what Python2 would do.
  649. For now, I consider Python2 to be the de facto main version of Python.
  650. Most scripts seem to be written in that version, not to mention that it&apos;s the default version of Python on one of the world&apos;s most-used Linux distributions: Debian.
  651. More popular operating systems such as Windows don&apos;t even have Python installed by default, so such systems don&apos;t even <strong>*have*</strong> a default to check.
  652. After a bit though, I changed my answer to the Python3-based answer because that&apos;s what the text book seems to use.
  653. Later, I came to a question about integer division though, which most people won&apos;t even run into in Python3.
  654. At that point, I switched my answer back to the Python2-based response.
  655. Now that I&apos;ve submitted the quiz, it&apos;s too late, and I&apos;m reflecting; I now remember that we learned about the integer division operator in Python3.
  656. Most likely, that question <strong>*was*</strong> about Python3, which means that I got it wrong.
  657. It&apos;d be nice if the quiz was more specific and told which version of Python was in use.
  658. In most cases, simple operators don&apos;t undergo huge changes between versions of a language, but in Python&apos;s case, they had a severe design flaw in the initial implementation and it needed to be fixed in a later version.
  659. As a result, it <strong>*does*</strong> matter which version of the language that you&apos;re using.
  660. </p>
  661. <p>
  662. My final thought for this week is probably that the course kept mentioning in several places use of a word processor for writing pseudocode.
  663. There is a major difference between a text editor and a word processor.
  664. A word processor is a horribly inefficient tool for working with plain text formats, such as that of pseudocode.
  665. Word processors are also usually overly-bloated, but that&apos;s not a hard requirement for being a word processor.
  666. It&apos;s true that you could write pseudocode using a word processor, but a text editor would be a much better option.
  667. Text editors are designed to work with plain text formats.
  668. Often times, they provide better tools for doing such, but even when they don&apos;t, they at least default to saving the file in the correct (<code>.txt</code>) format and they don&apos;t have all those text-file-incompatible options such as font-, size-, and color-formating.
  669. Word processors are almost always the wrong tool for a programmer.
  670. Word processors are built for formating formal documents, such as résumés and reports, and that&apos;s what they&apos;re good at.
  671. </p>
  672. <h3>Discussion board post drafts</h3>
  673. <blockquote>
  674. <p>
  675. Pseudocode is a way to express the plan for a program textually.
  676. It&apos;s often in a code-like format, but doesn&apos;t always adhere to an actual programming language.
  677. As a result, compiling or running pseudocode often doesn&apos;t work.
  678. Instead, it&apos;s sort of like written notes on the details of your program&apos;s process; it&apos;s like the roughest of rough drafts for an essay.
  679. </p>
  680. <p>
  681. A flow chart is a diagram that indicates the path that a program will take.
  682. It looks nothing like actual code, but it makes it clear how the program is intended to behave.
  683. </p>
  684. <p>
  685. There are three main reasons that I prefer pseudocode over flow charts.
  686. First off, pseudocode is a great representation of the way that I actually think.
  687. This doesn&apos;t apply to everyone, but I plan better in text formats than I do graphical formats.
  688. Second, a plain text editor is all that you need to write pseudocode.
  689. Flow charts require something more ... clunky.
  690. You could use an image editor such as the $a[GIMP] or you could use specialized software such as Dia.
  691. In either case, setting up the diagram is simply not as quick or as easy as just typing up what you plan to do.
  692. For me, speed is often essential, as I don&apos;t have much time to sit down and work.
  693. If I have to leave partway through the planning stage, I might forget the part of my plan that I haven&apos;t sketched out yet!
  694. Writing something quickly in pseudocode is a great way for me to express my ideas within a small time slot.
  695. I can even flesh them out better later if needed, as plain text documents are easier to edit than flow charts.
  696. Third, real code is also in a text format.
  697. Well-written pseudocode might be very close to being runnable and only need some minor changes before becoming the first actual implementation of your plan.
  698. By using pseudocode instead of a flow chat, I can reuse effort instead of starting again.
  699. </p>
  700. <p>
  701. Flow charts might be a better idea for the more graphically-inclined though.
  702. Some people visualize their programs graphically instead of textually.
  703. When showing your plan to someone else, a flow chart might be more useful that pseudocode as well.
  704. Pseudocode comes in a number of &quot;dialects&quot;, so reading the pseudocode of another person might not always be easy.
  705. For example, my pseudocode contains a high density of $a[PHP] tokens.
  706. People not familiar with $a[PHP] may have difficulties reading my pseudocode.
  707. Flow charts on the other hand, while they have some elements that make them easier to understand once you formally learn to produce them, are incredibly easy to read.
  708. They&apos;re <strong>*so*</strong> easy to read in fact that there exist flow charts that explain how to read flow charts; and they&apos;re effective for their target audience, people that don&apos;t know what flow charts are.
  709. </p>
  710. <p>
  711. As mentioned previously, pseudocode lacks much of the universality and intuitiveness of flow charts.
  712. I can also be a bit more verbose, which some people view as a bad thing.
  713. For the graphically-minded, it can also be less clear than a flow chart.
  714. </p>
  715. <p>
  716. The main drawback for me with flow chats is the need for special software to create and manipulate them.
  717. You can&apos;t simply use a basic text editor with them.
  718. Additionally, flow charts force you to put in a lot more effort.
  719. While you ony have to plan once, you&apos;re basically expressing your process twice, as you need to first express it graphically as a flow chart then later express it textually as actual code.
  720. </p>
  721. </blockquote>
  722. <blockquote>
  723. <p>
  724. I&apos;ve never heard of using pseudocode for documentation purposes.
  725. I&apos;ve only seen it used for planning and (when offering help to someone without knowing what language that they&apos;re working with) examples of how to structure a program.
  726. In what way would you use it in documentation?
  727. </p>
  728. </blockquote>
  729. <blockquote>
  730. <p>
  731. I like the way that you phrased that.
  732. Indeed, both flowcharts and pseudocode are ways of expressing algorithms.
  733. They don&apos;t express them in a way in which the computer can understand, but the ideas expressed are algorithms none the less.
  734. I also like your explanation as to when you&apos;d use each.
  735. Both pseudocode and flowcharts have their time and place.
  736. </p>
  737. </blockquote>
  738. <blockquote>
  739. <p>
  740. The fact that flowcharts are so hard to modify is a bit of a deterrent for using them, to be honest.
  741. However, they&apos;re easier to modify if you keep their &quot;source&quot;.
  742. I think that a flow chart is sort of like software in that reverse engineering them is more complicated than it needs to be.
  743. Instead of saving just a $a[PNG] version of the flowchart, you should also save the raw project.
  744. In <a href="apt:dia">Dia</a>, this would be the file with the <code>.dia</code> file extension.
  745. </p>
  746. </blockquote>
  747. <h2 id="Unit4">Unit 4</h2>
  748. <p>
  749. I started with the <a href="https://y.st./en/coursework/CS1101/tryme3.py.xhtml">programming assignment</a> again this week.
  750. The main difficulty in the assignment this week is the ambiguity.
  751. It says to define two functions.
  752. Defining them does not mean calling them.
  753. It says that the second function defined should be called on the last line of the file.
  754. Basically, this means that we need to define two functions and call one of them.
  755. However, the assignment also says that we should separate the output of the two functions to make it easier to grade.
  756. The first function was never called though, so there&apos;s nothing to separate.
  757. As such, I can only assume that we&apos;re supposed to actually call both functions.
  758. I hope that I don&apos;t get marked down for doing that.
  759. Aside from that, the only problems were simply minor incontinences.
  760. For example, I&apos;m used to being able to duplicate lines using &quot;control&quot; + &quot;d&quot;, but $a[IDLE] doesn&apos;t have that feature.
  761. I&apos;m also used to being able to insert tab characters using the &quot;tab&quot; key, but $a[IDLE] doesn&apos;t do that either, at least not by default.
  762. Only after completing the assignment for the week did I find the not-so-obvious setting that allowed me to reclaim my tab key and set it to its actual, intended purpose.
  763. </p>
  764. <p>
  765. When I tested the output to my script from within $a[IDLE], I found that instead of printing blank lines, <code>new_line()</code> was printing lines with pairs of parentheses!
  766. At first, I thought that Python was interpreting the parentheses at the end of the call to <code>print</code> as a string somehow.
  767. That didn&apos;t make any sense though; there were not quotation marks, no string delimiters.
  768. I remembered that the Python developers made a big deal about the <code>print</code> key word in Python2 being replaced by the <code>print()</code> function in Python3 though, so I tried removing the parentheses to see what would happen.
  769. While I can use the <code>python3</code> command on my command line to run things with Python3, $a[IDLE] (which the assignment instructions said to use when completing the assignment) is still using Python2.
  770. Sure enough, that fixed the problem!
  771. Now to try running it on the command line in Python3.
  772. The &quot;fix&quot; broke it for Python3, though the original version ran fine in Python3.
  773. I decided that without parentheses, Python3 must be interpreting my statement as an expression without an assignment.
  774. Functions in Python are contained in variables, so simply putting any valid in-use variable name on a line by itself with have the same effect: none.
  775. Likewise, I think that Python2 is interpreting the empty parentheses as a tuple or something.
  776. It&apos;s printing the string representation of a tuple, followed by a line feed.
  777. Changing the call to be <code>print(&quot;&quot;)</code> allows it to work as desired in both versions, providing cross-version compatibility.
  778. </p>
  779. <p>
  780. To be clear, I&apos;m using Python3 on the command line, where both Python2 and Python3 are available, but $a[IDLE] does not seem to have the option to go into Python3 mode.
  781. Because we&apos;re required to use $a[IDLE] for the assignments, I&apos;m stuck using Python2 for any test runs from within $a[IDLE].
  782. </p>
  783. <p>
  784. The entire reading assignment this week is from the textbooks mentioned in <a href="https://y.st./en/coursework/CS1101/#Unit1">Unit One</a>.
  785. It looks like this week, we learn the term &quot;composition&quot;.
  786. That&apos;s actually one of the terms that we were tested on last week.
  787. I got that question right because I&apos;ve been programming a while, but for beginning-level programmers, it&apos;s probably unfair to test them on terms before we get a chance to cover them.
  788. The textbook also also says that you &quot;should&quot; avoid using the same name for both a variable and a function.
  789. &quot;Should&quot; is the wrong word; more like &quot;must&quot;.
  790. &quot;Should&quot; implies that there is a choice.
  791. However, in Python, functions are objects, which means that functions are actually contained within variables.
  792. The book kind of hints at the fact that functions are objects, but doesn&apos;t even touch on the implications of this as far as variables go.
  793. If you give a function the same name as a variable, the variable is overwritten by the function definition.
  794. If you name a variable the same thing as a function, the reverse will happen and the variable&apos;s value will overwrite the function definition.
  795. It is not possible to have to variables with the same name (at least not within the same scope), so a function and a non-function variable cannot share a name in Python.
  796. Continuing, the book says that defining a function creates a variable of the same name, but this seems to imply that the variable is separate from the function, not acknowledging that the variable <strong>*contains*</strong> the function, and that without the variable, the function has no separate name.
  797. For example, take the code below:
  798. </p>
  799. <blockquote>
  800. <pre><code>def example():
  801. pass
  802. new_name = example
  803. del example
  804. new_name()
  805. example()</code></pre>
  806. </blockquote>
  807. <p>
  808. In the example above, <code>new_name()</code> will be called successfully.
  809. However, <code>example()</code> will not.
  810. It&apos;s variable was deleted, and without it, there is no way to access the function via that name.
  811. Instead, a Name Error will be raised.
  812. The book also mentions that the convention in Python is to indent by four spaces.
  813. This is true, but I have no idea why.
  814. <a href="https://y.st./en/opinion/code_indentation.xhtml">Spaces and tab characters both have different semantic value.</a>
  815. Semantically speaking, a space character is the wrong character for indention.
  816. Spaces, on the other hand, are word (or token) separators.
  817. The book also says that you can avoid certain problems caused by the mixing of tabs and spaces is to use spaces exclusively.
  818. This is <strong>*extremely*</strong> misleading.
  819. It makes it sound like the tab characters are problematic, when they&apos;re not.
  820. The same problem can be avoided by using tab characters exclusively and never indenting using space characters.
  821. More important than which character you use to indent with is that you pick a character and stick with it.
  822. Some people indent semantically, which means using the tab character.
  823. Some people indent with spaces instead.
  824. As long as you pick a convention and stick with it, you&apos;ll run into less bugs.
  825. The book also says that tab characters and space characters are invisible.
  826. This is exactly one of the reasons that I don&apos;t like $a[IDLE].
  827. In a reasonable text editor built with programming in mind, those characters are either visible by default or can be set to be visible.
  828. For example, check out the screenshot below.
  829. </p>
  830. <img class="weblog-header-image" src="/img/CC_BY-SA_4.0/y.st./coursework/CS1101/visible_whitespace.png" alt="visible whitespace" width="419" height="129" />
  831. <p>
  832. With a proper text editor, there is no confusion between tabs and spaces.
  833. Even line feed characters (and carriage return characters, when present) are visible!
  834. Personally, I use Geany, but Gedit is also known to have this functionality.
  835. Both offer syntax highlighting as well for several languages, including Python, and they both run on Linux and more popular systems such as Windows.
  836. For any Windows users, I also recommend Notepad++, which again, will show you whitespace and highlight your syntax.
  837. Unfortunately, Notepad++ is Windows-specific and won&apos;t run on Linux.
  838. </p>
  839. <p>
  840. I feel like I&apos;m nitpicking with this textbook a lot.
  841. If it were under a free culture license, I&apos;d probably fix the errors and send the corrections to the author (who might or might not actually incorporate them into the book).
  842. It&apos;s not my job to fix proprietary books though.
  843. </p>
  844. <p>
  845. We had less assignments to grade this week than last week.
  846. The first one that I graded was a flowchart.
  847. The main thing about it that I found noteworthy was that it greatly simplified the step of computing the answer based on the operator.
  848. In <a href="https://y.st./en/coursework/CS1101/calculator_flowchart.xhtml">my own flowchart</a>, I handle each operator separately.
  849. I think that I did this because as a programmer, I don&apos;t think that there&apos;s a way to handle this situation without branched code (which is what I simulated in my flowchart), the <code>eval()</code> function (which is messy and almost always the wrong way to do things), or a custom function (which is heavily overkill for this situation).
  850. However, a flowchart isn&apos;t code.
  851. It doesn&apos;t have to play by the same rules as code.
  852. It might have been better for me to adopt a simpler, higher-level view in my flowchart like this other student had.
  853. </p>
  854. <p>
  855. The next submission was pseudocode, but I couldn&apos;t understand what it was trying to say.
  856. Strange indention made it difficult to decipher,
  857. The code began with an indented block with no particular reason to indent, for example.
  858. The <code>while</code> statement beginning the first understandable block was then unindented.
  859. I finally deciphered a part near the end to be a set of nested <code>if</code> statements, where each new <code>if</code> block was in the previous one&apos;s <code>else</code> block.
  860. if a basic <code>elseif</code> was used instead, it would be easier to understand with the formatting that it had.
  861. Using <code>if</code> from within <code>else</code> is fine, but in that case, each new <code>if</code> should be indented one more level than the last to make it readable.
  862. At the end of the pseudocode, I think that the author intended the process to loop back to the beginning under some conditions, but instead, it just asks for input again then halts.
  863. Other parts of the code are nested when they shouldn&apos;t be.
  864. I hate to be a nag, but after I finally managed to decipher the pseudocode, I found that it didn&apos;t meet half of the requirements: those of checking the input for problems.
  865. </p>
  866. <p>
  867. The final submission that I graded was also pseudocode and also didn&apos;t do any error checking.
  868. however, it was much more concise, and the little indentation that was used was used in exactly the right places.
  869. While it took several minutes to figure out what the last submission did, this one could be understood in a glance.
  870. </p>
  871. <p>
  872. I didn&apos;t do as well on the ungraded quiz as I&apos;d like to have, but I still did alright.
  873. I got one question wrong because I wasn&apos;t quite sure what it was asking.
  874. The other two that I got wrong were due to my genuine lack of knowledge in Python.
  875. Due to recent events, I should have a lot more time to study in coming weeks though.
  876. </p>
  877. <p>
  878. The discussion board also went smoother than in past weeks, and I was able to submit my first post fairly early on this time.
  879. It looks like I was able to put to words something that one of the other students had been thinking, which was nice.
  880. </p>
  881. <p>
  882. Unfortunately, I wasn&apos;t able to complete the exercises this week.
  883. I normally do them, and I might do this week&apos;s exercises next week to catch up.
  884. This week was very demanding for me, both physically and emotionally, so as the exercises were ungraded, they had to be put aside.
  885. That said, my final thought for the week is that a great burden was lifted from my shoulders in these past couple days.
  886. THis should free me up much more to pursue my education and excel in class.
  887. </p>
  888. <h3>Discussion board post drafts</h3>
  889. <blockquote>
  890. <p>
  891. A parameter is a type of local variable name that exists only within the scope of the function that it&apos;s defined in.
  892. In many ways, it&apos;s the same as any other local variable defined within the context of the function.
  893. However, parameters have a special property as well.
  894. They have no initial value, and their name binds to a value passed as an argument into the function.
  895. This allows the function to be defined to work with data passed into it instead of always outputting identical information or performing identical actions.
  896. </p>
  897. <p>
  898. I suppose that in a way, you could say that a parameter is a variable name with no value.
  899. A parameter is a value with no variable name.
  900. When you call a function, the values are bound to the variable names to form local variables until the function completes.
  901. </p>
  902. <p>
  903. Of course, a variable could be passed in a function call, but it&apos;s not really the variable that&apos;s being passed into as an argument at all.
  904. Instead, it&apos;s the value of that variable that gets used as the argument.
  905. Any valid expression can be passed in a function call because the expression itself isn&apos;t what gets passed as an argument.
  906. Instead, expressions are evaluated to find their value, and it&apos;s that value that&apos;s actually being used as an argument.
  907. The reason that variables can be used as arguments in function calls is because when evaluated, the result is their value.
  908. Function calls can also be passed in because they&apos;re evaluated and result in their return value.
  909. The same applies of course to mathematical equations and anything else that can be evaluated in Python.
  910. </p>
  911. </blockquote>
  912. <blockquote>
  913. <p>
  914. If you&apos;re talking about calling a function from within a function, and using a parameter of the outer function as an argument for the calling of the inner function, then yes, you can do that.
  915. You can even call the same function, though I don&apos;t recommend doing that with identical input (such as by passing the parameter as an argument).
  916. You can treat the parameter as you would any other local variable.
  917. You can call the parameter <strong>*as*</strong> a function, you can pass it as an argument <strong>*to*</strong> a function, and you can even assign a new value to that variable.
  918. </p>
  919. </blockquote>
  920. <blockquote>
  921. <p>
  922. Code reuse is probably the main and most important feature of user-defined functions.
  923. It&apos;s helpful not to have to write the same code several times or write it once and paste it into several places.
  924. Additionally, code reuse from functions allows you to update only a single copy of the code and have every instance of its use in your script be updated as well.
  925. It&apos;s common also to use it to make code look simpler and easier to read.
  926. </p>
  927. <p>
  928. One topic with functions that I hope that we cover is recursion.
  929. So far, what we&apos;ve done with user-defined functions could be accomplished without them, but our functions make the code easier to read and update.
  930. However, recursion is something that cannot really be accomplished in Python without user-defined functions.
  931. Essentially, it allows you to have your script work its way through structures that are nested arbitrarily deep.
  932. It might not make much sense now, but it&apos;ll be a vital tool later on when programming certain larger projects.
  933. </p>
  934. </blockquote>
  935. <blockquote>
  936. <p>
  937. That&apos;s a useful way to think about parameters.
  938. They are like settings for the function&apos;s code block to work with.
  939. The same code block may need to run again with different settings, so different arguments would need to be passed in.
  940. </p>
  941. </blockquote>
  942. <blockquote>
  943. <p>
  944. Easier debugging is an important aspect of code reuse, such as that of user-defined functions.
  945. User-defined functions can also help you get started on code that you don&apos;t know how to write yet.
  946. For example, you might have a pretty good idea of what you want your code to do, but you might still be unsure how to implement specific parts.
  947. You can define dummy functions that don&apos;t do anything and use them in your code as it they did what you need done.
  948. Later, you can go back and define real functions, and your code will automatically use the real functions and you&apos;ll get the result that you are after.
  949. </p>
  950. </blockquote>
  951. <h2 id="Unit5">Unit 5</h2>
  952. <p>
  953. My professor has freed me from $a[IDLE]!
  954. I no longer have to use it for assignments.
  955. It was interesting to get to try it, but it&apos;s not an editor that I recommend for serious use.
  956. </p>
  957. <p>
  958. The programing assignment for this week is to take a conceptual model and turn it into an actual program.
  959. Two conceptual models are provided, one is pseudocode and one as a flowchart.
  960. This seems like an easy task until you notice that the two models contradict one another.
  961. The basic issue is that the pseudocode allows the use of zeros while the flowchart does not.
  962. The obvious answer is to take all the programmable qualities of both models, which means disallowing zeros, but the contradiction doesn&apos;t stop there.
  963. The pseudocode uses an invalid operator as a signal to exit the program.
  964. The flowchart instead prompts for a new operator.
  965. If this were my own project, the solution would be easy.
  966. Prompt for valid operators and only check for zeros as divisors in division (zeros anywhere else are fine).
  967. This is graded though, and I can&apos;t see the grading criteria until it&apos;s too late.
  968. I ended up going with the flowchart version, as it exemplifies the hypothetical calculator we were working with two weeks ago, so it would be more likely to fit the grading rubric.
  969. </p>
  970. <p>
  971. It was kind of fun to set up the prompts.
  972. I also experimented with indention to see how picky Python would be.
  973. I don&apos;t like to indent comments, and in my opinion, I shouldn&apos;t have to.
  974. Python agrees.
  975. Python also allowed me to not indent empty lines.
  976. As long as a given line didn&apos;t have any instructions for Python to follow on it, it ignored the indention level.
  977. First, I wanted to check to see if my input was a number.
  978. You can never trust input from users, and trying to do math with strings would be problematic.
  979. Before I&apos;d put together a solution though, I realized that <strong>*all*</strong> input from users will be strings!
  980. I needed a way not only to weed out bad input, but also to parse the input.
  981. Proper output would require that I treat integers and floats, both input as strings, differently.
  982. <code>5 * 2</code> should output <code>10</code>, not <code>10.0</code>, while <code>3 * 2.5</code> should output <code>7.5</code>.
  983. </p>
  984. <p>
  985. After completing the code, debugging in Python3 as I went, it was time to test it in Python2.
  986. A good programmer would make the code run in both versions.
  987. Besides, it&apos;s a learning experience.
  988. Running the script in Python2 raised strange errors though.
  989. It took forever to figure out what the problem was.
  990. It turns out that Python3 does away with Python2&apos;s <code>input()</code> function and renames Python2&apos;s <code>raw_input()</code> function to be Python3&apos;s <code>input()</code> function.
  991. Python2&apos;s <code>input()</code> function tries to evaluate the user&apos;s input as an expression!
  992. One should never trust user input like that.
  993. Assuming that the user only input valid numbers, it would have removed the need to convert string input into numbers, but the cost is too high.
  994. Likewise, operators couldn&apos;t be input without quotation marks around them unless I used <code>raw_input()</code>, and Python3 doesn&apos;t have a function by that name.
  995. In Python though, everything that isn&apos;t a language construct is an object.
  996. That means that functions are objects.
  997. All I had to do was check for the presence of <code>raw_input</code> and rename it <code>input</code> if it existed.
  998. This (along with <code>from __future__ import division</code>) upgraded Python2 to behave like Python3, so the <a href="https://y.st./en/coursework/CS1101/mycalc.py.xhtml">script</a> now runs in both.
  999. </p>
  1000. <p>
  1001. Now, I had the problem of the output file: I was supposed to include one.
  1002. The script takes user input though, so I can&apos;t enumerate all output that it produces.
  1003. I ended up just showing a session in which I tried to enter invalid input and divide by zero (to show the error messages), then divided by two (to show the displayed result and exit the script).
  1004. </p>
  1005. <p>
  1006. After completing the discussion assignment, I graded assignment submissions from last week.
  1007. It seems that I&apos;m not the only one that trouble getting <code>print()</code> to function!
  1008. The first submission that I graded used the <code>print</code> key word from Python2.
  1009. The script doesn&apos;t have any output at all in Python3.
  1010. I gave it full credit, but explained in the feedback comments how to make it work better next time.
  1011. The next submission used the string &quot;----------------------------------------------------------------------&quot; instead of blank lines.
  1012. What were they thinking?
  1013. Do they not know what a <strong>*blank*</strong> line is?
  1014. More to the point, did they not read the predefined definition of <code>blank_line()</code>?
  1015. In any case, I had to mark them down for not following directions.
  1016. The final submission I graded was almost the perfect implementation.
  1017. It used loops, which I&apos;d considered doing, but decided not to.
  1018. The loop would have taken up as many lines as simply calling the function three times would, in the case of <code>nine_lines()</code>, and I called <code>nine_lines()</code> twice from within <code>clear screes()</code>, along with <code>three_lines()</code> twice and <code>new_line()</code> once so that function could build off the function before it (<code>nine_lines()</code>), so a loop over <code>nine_lines()</code> and <code>three_lines()</code> would have been overkill.
  1019. This submission instead used the <code>range()</code> function to create arrays to iterate over, one in each of the two main functions.
  1020. <code>clear_screen()</code>&apos;s loop called <code>new_line()</code> instead of having anything to do with <code>nine_lines()</code>.
  1021. I think a <code>while</code> loop would be better than a <code>for</code> loop, as the array created was entirely ignored in the loop, but the submitted solution was elegant none the less.
  1022. </p>
  1023. <p>
  1024. The reading assignment for the week included chapters from both textbooks mentioned in <a href="https://y.st./en/coursework/CS1101/#Unit1">Unit 1</a>, as well as a video about <a href="https://www.youtube.com/watch?v=D0Nb2Fs3Q8c">while loops in Python</a>.
  1025. By the time I got to the reading assignment, I was <strong>*way*</strong> late to start the reading.
  1026. The events of last week set me back by two days, and I was able to recover one of them that week.
  1027. This week&apos;s been about recovering the second day, which I was able to do, so I can start next week with a clean slate.
  1028. Mostly.
  1029. I&apos;m still behind in the ungraded exercises.
  1030. </p>
  1031. <p>
  1032. The textbook presented an interesting use case for modulus division.
  1033. It mentioned using it to see if an integer is divisible by another integer, which I think is a pretty common use case, but it also mentioned using it to extract the least-significant digits of an integer.
  1034. I&apos;m not sure what that&apos;d be good for off the top of my head, but it sounds like something that could be useful in some corner cases.
  1035. </p>
  1036. <p>
  1037. Finally, with the reading complete, I took the ungraded quiz to finish for the week.
  1038. Thankfully, this quiz was all about functionality and involved no history questions of any kind.
  1039. Being bad at remembering history doesn&apos;t make me a bad programmer, but it makes me look like one when I&apos;m getting answers wrong on a basic programming course quiz because it asks history questions instead of programming questions.
  1040. </p>
  1041. <h3>Discussion board post drafts</h3>
  1042. <blockquote>
  1043. <p>
  1044. The <code>=</code> and <code>==</code> operators perform very different roles, but have some similarities.
  1045. It&apos;s worth noting that both operators have return values, just like functions.
  1046. (A function&apos;s return value, if not explicitly defined, is <code>None</code>.
  1047. A function always returns a value though.)
  1048. </p>
  1049. <p>
  1050. <code>==</code> is an operator used in an expression that tests for equality of two subexpressions.
  1051. The return value is either <code>True</code> if the subexpressions are equal or <code>False</code> if they aren&apos;t.
  1052. This expression and return value can be used anywhere that another value could be used.
  1053. For example, <code>if False == True:</code> is valid Python, as long as it begins a block.
  1054. <code>my_variable = q == v</code> is also valid, and assigns a <code>True</code> or <code>False</code> value to <code>my_variable</code>.
  1055. </p>
  1056. <p>
  1057. That brings us to the assignment operator, <code>=</code>.
  1058. The subexpression on the left side of the operator must be a variable name, and the operator assigns the value of the subexpression on right to that variable.
  1059. Strangely though, this operator still returns a value: the value that it assigned to the variable.
  1060. That means that this is valid in Python: <code>var_x = var_y = 2 + 3</code>.
  1061. It assigns the value <code>5</code> to <code>var_y</code>, then assigns the value returned by <code>var_y = 2 + 3</code> (which is also <code>5</code>) to <code>var_x</code>.
  1062. Strangely, this is invalid in Python, but works in other languages: <code>if var_z = 5:</code>.
  1063. In theory, it should assign the value of <code>5</code> to <code>var_z</code>, then evaluate <code>5</code> as <code>True</code> and run the block of code.
  1064. Most likely, Python disallows this because it&apos;s a common mistake to type that and there&apos;s little use for using an assignment operator in the conditional test of an <code>if</code> statement.
  1065. </p>
  1066. <p>
  1067. Long story short, if you&apos;re trying to set the value of a variable, use the assignment operator, <code>=</code>.
  1068. If you&apos;re trying to check for equality, use the equality operator instead, <code>==</code>.
  1069. This will often, though not always, be used for loops and conditional blocks.
  1070. </p>
  1071. </blockquote>
  1072. <blockquote>
  1073. <p>
  1074. You&apos;re right, it&apos;s good to note that a variable can only hold one value at a time.
  1075. Assigning a new value causes the old one to be lost.
  1076. It&apos;s also worth noting that because Python is a weakly-typed language, a variable might hold different types of values at different points in the script.
  1077. For example, a variable that holds an integer value might be assigned a string value upon next value assignment.
  1078. </p>
  1079. </blockquote>
  1080. <blockquote>
  1081. <p>
  1082. That&apos;s a nice concise, clear explanation with examples.
  1083. You might want to put the pound sign before the parenthesis though to include it in the comment.
  1084. Additionally, in Python, True and False begin with capital letters.
  1085. Otherwise, Python thinks that you mean user-defined variables called "true" and "false".
  1086. </p>
  1087. </blockquote>
  1088. <blockquote>
  1089. <p>
  1090. I like how you point out that the assignment operator, <code>=</code>, isn&apos;t only used to assign values to variables.
  1091. It can also assign values to elements in an array or properties of an object.
  1092. </p>
  1093. </blockquote>
  1094. <h2 id="Unit6">Unit 6</h2>
  1095. <p>
  1096. My challenge for the week was figuring out how to properly complete the programing assignment.
  1097. The instructions seem even more self-contradictory and ambiguous that usual.
  1098. They say that the function should prompt for user input, but also that it should get its data from arguments instead.
  1099. I decided to have the function have parameters, but have the script prompt for input to pass into the function as arguments.
  1100. I&apos;m hoping that&apos;s what the assignment meant to do.
  1101. </p>
  1102. <p>
  1103. As usual, pseudocode would be the easier way to plan in this assignment, but a flowchart has three advantages: it won&apos;t be mistaken as actual code, I won&apos;t need to intentionally downgrade my pseudocode style to mitigate the first problem, and the flowchart makes for a more interesting thing to look at in my archive.
  1104. I wanted to create a subprocess in the flowchart for the function in the script, a subprocess with three ending points; the function itself would have three <code>return</code> statements, so why shouldn&apos;t the flowchart reflect that?
  1105. I couldn&apos;t remember for sure if that was allowed though.
  1106. It <strong>*should*</strong> be allowed, but going back to <a href="https://y.st./en/coursework/CS1101/#Unit3">Unit 3</a>, I couldn&apos;t find any indication that it <strong>*is*</strong> allowed.
  1107. I decided to play it safe, and use a single ending point in the subprocess.
  1108. Doing so required adding a variable to the flowchart that would never be added to the actual script.
  1109. </p>
  1110. <p>
  1111. I was a step away from completing my code, but I had to check something really quickly first.
  1112. Obviously, the user input needed to be converted to a numeric type before being compared, but some user input can&apos;t be converted to integers or floating points.
  1113. I have a habit of poking things.
  1114. I check corner cases on all sorts of things, even when it&apos;s not code and even when it&apos;s not something I&apos;m building.
  1115. This does carry over to my code-writing though.
  1116. Instead of adding the type conversion I needed to complete my script, I instead ran it as-is.
  1117. I tried to compare &quot;q&quot; to &quot;a&quot; to watch the script break.
  1118. ... but it didn&apos;t break.
  1119. Strings can be compared using the <code>&gt;</code>, <code>==</code>, and <code>&lt;</code> operators in Python!
  1120. Comparing single-digit number strings even provides the expected results.
  1121. Comparing multi-digit number strings doesn&apos;t work as expected though.
  1122. Or to be more precise, it works <strong>*exactly*</strong> as expected.
  1123. Numeric strings are strings, and these comparison operators seem to basically function to alphabetize strings.
  1124. That means that the string &quot;20&quot; is less than the string &quot;3&quot;.
  1125. <strong>*This is as it should be.*</strong>
  1126. However, it raises an interesting question: should my script be comparing strings or comparing numbers?
  1127. The input the assignment specifies is single-digit number strings, so either way, the output will remain the same.
  1128. That means that this decision shouldn&apos;t affect my grade; the question is of which is the right way to do things, not which will result in getting credit for the assignment.
  1129. After going back and forth a while, I decided to add a prompt at the beginning to allow users to choose which type of comparison to have the script make, then implemented both options.
  1130. </p>
  1131. <p>
  1132. With the <a href="https://y.st./en/coursework/CS1101/bool.py.xhtml">script</a> complete and working in Python3, I tried it in Python2.
  1133. This time, Python2&apos;s <code>print</code> key word was reading my multiple arguments to the Python3 <code>print()</code> function as a tuple.
  1134. I could remove the parentheses, but that caused a syntax error error in Python3.
  1135. In other words, I couldn&apos;t conditionally call <code>print()</code> in Python3 and use the <code>print</code> key word without parentheses in Python2, as merely having a line with the Python2 usage would prevent the script from running in Python3.
  1136. I ended up using concatenation to fix it.
  1137. It&apos;s not pretty, but it makes the code portable.
  1138. </p>
  1139. <p>
  1140. When I went to submit my assignment this week, I quickly noticed the usual format-it-as-a-Microsoft-Word-document requirement was absent this week.
  1141. In every other course at I&apos;ve taken at University of the People, OpenOffice documents have also been accepted.
  1142. More importantly though, the submission form has an actual <code>&lt;textarea/&gt;</code> each week.
  1143. Why not make use of that?
  1144. It&apos;s much easier for graders, as we don&apos;t have to download the assignment files to our hard drives before grading; we can simply read the assignment submission on the same Web page as the grading form.
  1145. I did upload my script as a file in case someone wants to run it, but the flowchart, script, and output are all visible without a download as well.
  1146. </p>
  1147. <p>
  1148. Exercise 7.5 this week asks us to implement some complex mathematical formula in Python, but I have no idea how to do that.
  1149. I don&apos;t even understand the formula!
  1150. It uses symbols I&apos;m unfamiliar with.
  1151. As this is a textbook on programming, not maths, you&apos;d think it wouldn&apos;t be throwing complex mathematical expressions at us without explaining how they work.
  1152. Sheesh.
  1153. </p>
  1154. <p>
  1155. There wasn&apos;t anything particular noteworthy in the reading assignment this week aside from the complex formula.
  1156. It was all pretty straightforward.
  1157. In addition to some chapters in our textbooks to read, we also had two videos to watch:
  1158. </p>
  1159. <ul>
  1160. <li>
  1161. <a href="https://www.youtube.com/watch?v=6qCQB8E5bkI">Diagramming What Happens with a Function Call</a>
  1162. </li>
  1163. <li>
  1164. <a href="https://www.youtube.com/watch?v=JwO_25S_eWE">Defining a Factorial Function</a>
  1165. </li>
  1166. </ul>
  1167. <p>
  1168. The videos seemed overly-complex, but maybe that&apos;s just me.
  1169. I learn better from reading text than watching videos.
  1170. All of it was stuff I knew though, so I followed along just fine, it just seemed like there were quicker and easier ways to explain things.
  1171. I got this week&apos;s reading assignment done a little early, so I was able to go back and complete some of the exercises from the past two units that I&apos;d had to skip for the time being.
  1172. However, I had no days off from work this week, so I wasn&apos;t able to complete it all.
  1173. One of the exercises I was supposed to complete last unit taught me something about triangles.
  1174. There&apos;s something called a degenerate triangle, and the sum of two of the sides are equal to that of the remaining side.
  1175. I didn&apos;t think that was actually considered a triangle.
  1176. I mean, clearly it&apos;s flat and two of the sides combine into one; one side that perfectly overlaps the other side.
  1177. What kind of triangle is that?
  1178. (That was rhetorical; obviously the answer is that it&apos;s a degenerate triangle.)
  1179. Also from what I caught up on from last week, I needed to install <a href="http://greenteapress.com/thinkpython/swampy/">Swampy</a>.
  1180. Swampy depends on Tkinter, which seems to already be installed on my system, but only the Python2 version of it was present.
  1181. To get it working in Python3, I needed to run <code>sudo aptitude install <a href="apt:python3-tk">python3-tk</a></code> on the command line.
  1182. Next, I needed to install Swampy, using the command <code>sudo pip install swampy</code>.
  1183. I&apos;m always very uneasy about installing software from outside the package manager like this.
  1184. I mean, I guess pip is another package manager, but using one and only one package manager seems like the safest and most stable option.
  1185. When I checked on the install, I saw it failed.
  1186. It couldn&apos;t reach the remote file.
  1187. I&apos;d totally forgotten that I don&apos;t have a &quot;real&quot; Internet connection at home.
  1188. Pip was no doubt trying to make a proxyless connection to the network, which isn&apos;t possible from here.
  1189. Everything network-related on my laptop has to be run through my mobile&apos;s $a[Tor] instance, or it won&apos;t go through.
  1190. Running <code>sudo torsocks pip install swampy</code> fixed the problem, and I was able to install Swampy.
  1191. That only worked to install it for Python2 though; Python3 still couldn&apos;t access the <code>swampy</code> module.
  1192. At that point, I gave up and just decided to work on the exercises in Python2.
  1193. I ran out of time to actually get anywhere with the turtle exercise that uses Swampy though.
  1194. </p>
  1195. <p>
  1196. Two of the assignments submissions I graded this week were fine examples.
  1197. One though ... one was full of problems.
  1198. Like ... All I can think is that they didn&apos;t actually try running their script before submitting it, even.
  1199. The output file is incomplete, the script doesn&apos;t check for zeros in the right places, though it does check for zeros in a wrong place ... it even specifies that they user may enter any of nine operators, though five of them don&apos;t actually <strong>*do*</strong> anything ...
  1200. </p>
  1201. <p>
  1202. Lastly, I took the ungraded and graded quizzes.
  1203. On the ungraded quiz, this question appeared:
  1204. </p>
  1205. <blockquote>
  1206. <p>
  1207. A loop where the terminating condition is never achieved is called an _______
  1208. </p>
  1209. </blockquote>
  1210. <p>
  1211. One of the options was:
  1212. </p>
  1213. <blockquote>
  1214. <p>
  1215. d. for .. ever loop
  1216. </p>
  1217. </blockquote>
  1218. <p>
  1219. That gave me a good laugh!
  1220. Another question asked about the execution of code, and used the <code>+=</code> operator.
  1221. Python has that!?
  1222. I thought I remembered being taught that such an operator wasn&apos;t present in Python, which was a mild inconvenience, seeing as I&apos;m used to using it all the time in $a[PHP].
  1223. I tested it out though, and both <code>+=</code> and <code>-=</code> are implemented in both Python2 and Python3.
  1224. I was either taught incorrectly before or I misremembered.
  1225. In any case, I had need for decrementing in my code submission for the week, so I fixed my code which previously read <code>iterations = iterations - 1</code> to do things the <strong>*right*</strong> way: <code>iterations -= 1</code>.
  1226. </p>
  1227. <p>
  1228. Life has settled into a better-than-normal state, or at least a better-than-was-previously-normal state.
  1229. I have no doubt that it&apos;ll take me some time to fully recover, but even just now, I have so much more time and energy for my studies than I&apos;ve had in the past.
  1230. The future, or my educational future at the very least, is looking bright.
  1231. </p>
  1232. <h3>Exercises</h3>
  1233. <h4>From <a href="https://y.st./en/coursework/CS1101/#Unit5">Unit 5</a></h4>
  1234. <h5>Exercise 5.1</h5>
  1235. <blockquote>
  1236. <pre><code>def check_fermat(a, b, c, n):
  1237. if n &lt; 2 and a**n + b**n == c**n:
  1238. print(&quot;Holy smokes, Fermat was wrong!&quot;)
  1239. else:
  1240. print(&quot;No, that doesn&apos;t work.&quot;)
  1241. def prompt():
  1242. print(&quot;The goal is to find integer values for a, b, c, and n, where n is greater than two and this equation holds true:\\n\\ta**n + b**n == c**n\\n&quot;)
  1243. a = int(input(&quot;Please enter a value for a:\\n&gt;&gt;&gt; &quot;))
  1244. b = int(input(&quot;Please enter a value for b:\\n&gt;&gt;&gt; &quot;))
  1245. c = int(input(&quot;Please enter a value for c:\\n&gt;&gt;&gt; &quot;))
  1246. n = int(input(&quot;Please enter a value for n:\\n&gt;&gt;&gt; &quot;))
  1247. check_fermat(a, b, c, n)</code></pre>
  1248. </blockquote>
  1249. <h5>Exercise 7.1</h5>
  1250. <blockquote>
  1251. <pre><code>def print_n(s, n):
  1252. while n:
  1253. print(s)
  1254. n = n - 1</code></pre>
  1255. </blockquote>
  1256. <h4>From <a href="https://y.st./en/coursework/CS1101/#Unit6">Unit 6</a></h4>
  1257. <h5>Exercise 6.1</h5>
  1258. <p>
  1259. Um ...
  1260. That&apos;s pretty much the <a href="https://y.st./en/coursework/CS1101/bool.py.xhtml">assignment for the week</a>.
  1261. </p>
  1262. <h5>Exercise 6.2</h5>
  1263. <blockquote>
  1264. <pre><code>import math
  1265. def hypotenuse(a, b):
  1266. return math.sqrt(a**2+b**2)</code></pre>
  1267. </blockquote>
  1268. <p>
  1269. (The exercise says to record each step of the process of developing this, but it was too simple of an exercise to require more than one step to develop.
  1270. Simply writing the three lines above creates a functional function.)
  1271. </p>
  1272. <h5>Exercise 6.3</h5>
  1273. <blockquote>
  1274. <pre><code>def is_between(x, y, z):
  1275. if x &gt; y:
  1276. return False
  1277. elif y &gt; z:
  1278. return False
  1279. else:
  1280. return True</code></pre>
  1281. </blockquote>
  1282. <h5>Exercise 6.5</h5>
  1283. <blockquote>
  1284. <pre><code>def ack(m, n):
  1285. if m == 0:
  1286. return n + 1
  1287. elif m &gt; 0 and n == 0:
  1288. return ack(m-1, 1)
  1289. elif m &gt; 0 and n &gt; 0:
  1290. return ack(m-1, ack(m, n-1))
  1291. print(ack(3, 4))</code></pre>
  1292. </blockquote>
  1293. <p>
  1294. Incrementing <code>m</code> by one or <code>n</code> by three is enough to cause a recursion depth error.
  1295. </p>
  1296. <h5>Exercise 6.6</h5>
  1297. <blockquote>
  1298. <pre><code>def first(word):
  1299. return word[0]
  1300. def last(word):
  1301. return word[-1]
  1302. def middle(word):
  1303. return word[1:-1]
  1304. def is_palindrome(string):
  1305. if len(string) == 0:
  1306. return True
  1307. elif first(string) == last(string):
  1308. return is_palindrome(middle(string))
  1309. else:
  1310. return False</code></pre>
  1311. </blockquote>
  1312. <h5>Exercise 6.7</h5>
  1313. <p>
  1314. This exercise doesn&apos;t provide a base <code>True</code>case.
  1315. If <code>a</code> is not divisible by <code>b</code>, <code>False</code> is returned.
  1316. Otherwise, <code>is_power(a/b, b)</code> is returned.
  1317. At some point, <code>a</code> will be too small to be divisible by <code>b</code> and <code>False</code> will be returned.
  1318. I&apos;ve added a stipulation to my function to say that if <code>a</code> is equal to <code>b</code>, it&apos;s a power of it, but that&apos;s technically not as the instructions said to do.
  1319. </p>
  1320. <blockquote>
  1321. <pre><code>def is_power(a, b):
  1322. if a == b:
  1323. return True
  1324. elif a % b == 0:
  1325. return is_power(a//b, b)
  1326. else:
  1327. return False</code></pre>
  1328. </blockquote>
  1329. <h5>Exercise 6.8</h5>
  1330. <blockquote>
  1331. <pre><code>def gcd(a, b):
  1332. if b == 0:
  1333. return a
  1334. else:
  1335. return gcd(b, a%b)</code></pre>
  1336. </blockquote>
  1337. <h5>Exercise 7.2</h5>
  1338. <blockquote>
  1339. <pre><code>def square_root(a):
  1340. epsilon = 0.0000001
  1341. x = a
  1342. while True:
  1343. y = (x + a/x) / 2
  1344. if abs(y-x) &lt; epsilon:
  1345. break
  1346. x = y
  1347. return y</code></pre>
  1348. </blockquote>
  1349. <h5>Exercise 7.3</h5>
  1350. <blockquote>
  1351. <pre><code>import math
  1352. def square_root(a):
  1353. epsilon = 0.0000001
  1354. x = a
  1355. while True:
  1356. y = (x + a/x) / 2
  1357. if abs(y-x) &lt; epsilon:
  1358. break
  1359. x = y
  1360. return y
  1361. def test_square_root():
  1362. i = 0
  1363. while i &lt; 9:
  1364. i = i + 1
  1365. # Floats are unreliable, so I don&apos;t feel comfortable incrementing a
  1366. # float. Instead, let&apos;s store and increment an integer, then cast to a
  1367. # float.
  1368. col0 = float(i)
  1369. col1 = square_root(col0)
  1370. col2 = math.sqrt(col0)
  1371. col3 = abs(col1-col2)
  1372. # The exercise doesn&apos;t explain how it wants the data formatted, but
  1373. # this setup makes the formatted data look like the example, so that&apos;s
  1374. # what I&apos;m going with. However, it&apos;s a bit imprecise, as it does
  1375. # involve rounding. The rounding also makes the table look dead wrong
  1376. # in some spots, because the difference between the second and third
  1377. # columns looks like it should be zero.
  1378. col1 = str(round(col1, 11))[:13].ljust(13)
  1379. col2 = str(round(col2, 11))[:13].ljust(13)
  1380. print (col0, col1, col2, col3)</code></pre>
  1381. </blockquote>
  1382. <h5>Exercise 7.4</h5>
  1383. <blockquote>
  1384. <pre><code># What a horrid function this is! One must never trust user input
  1385. # enough to simply eval() it!
  1386. def eval_loop():
  1387. # The user might enter &quot;done&quot; right away without entering other
  1388. # expressions first. To account for that, we need a starting value for
  1389. # our return variable.
  1390. val = None
  1391. command = input(&quot;&gt;&gt;&gt; &quot;)
  1392. while command != &quot;done&quot;:
  1393. val = eval(command)
  1394. print(val)
  1395. command = input(&quot;&gt;&gt;&gt; &quot;)
  1396. return val</code></pre>
  1397. </blockquote>
  1398. <h5>Exercise 7.5</h5>
  1399. <p>
  1400. ...
  1401. </p>
  1402. <p>
  1403. This is a programming course, not an advanced mathematics course.
  1404. I don&apos;t even understand the formula; there&apos;s no way I can implement it.
  1405. That large symbol in the middle looks critical, but I don&apos;t even know what it means.
  1406. I only learned about factorials this week, too; I had no idea what the exclamation point meant in maths before that.
  1407. </p>
  1408. <h3>Discussion post drafts</h3>
  1409. <blockquote>
  1410. <p>
  1411. The main thing that I use functions for is code reuse, in both senses of the term.
  1412. I write a function once, then import it into multiple scripts.
  1413. I might even use a function multiple times from within the same script.
  1414. Using functions from within multiple scripts requires that functions be defined in separate files than the main script, but it&apos;s well worth it.
  1415. </p>
  1416. <p>
  1417. Parameters allow me to write less functions to cover more use cases and write functions for more types of situations.
  1418. Often times, one function can cover more cases if its inner workings can be adjusted slightly.
  1419. Parameters are great for this, as they can be used to alter one of the function&apos;s local variables, changing the output.
  1420. Additionally, parameters are great for specifying what data a function should operate on.
  1421. A function that provides identical output every time is almost useless; you might as well replace the function call with its output.
  1422. Parameters provide a clean way to feed your function the data you want it to process.
  1423. Almost every function that I write has parameters for this very reason.
  1424. </p>
  1425. <p>
  1426. Return values are often vital, especially when trying to generalize your code.
  1427. Functions that process data shouldn&apos;t do anything with the result, as you might need something different done with the result next time.
  1428. Perhaps one time, you want to pass the result to the <code>print()</code> function, but in a different script or different part of the same script, you want write the result to a database.
  1429. Instead of doing either of these, the function should return the result and allow other parts of the code to decide what to do with the return value.
  1430. This provides a level of versatility that increases our ability to reuse functions instead of rewriting them.
  1431. </p>
  1432. <p>
  1433. Some people also argue that using functions helps make code self-documenting.
  1434. You can name a piece of code, it can be read and understood, then you can later use it in a concise manner in your main script without a human reader having to go through the steps of what each statement of the function does.
  1435. Functions can also be used as placeholders.
  1436. You can write up your main script, calling functions that don&apos;t exist yet from within it.
  1437. Once finished, you can figure out how to implement those parts you haven&apos;t written yet, and write them as the missing functions.
  1438. </p>
  1439. </blockquote>
  1440. <blockquote>
  1441. <p>
  1442. I don&apos;t think functions help hide information in programs, they just move the information elsewhere.
  1443. It&apos;s helpful for making the program more readable, but the information is all still there and unhidden.
  1444. Functions do help us not repeat ourselves while still being able to use the same algorithm repeatedly though.
  1445. </p>
  1446. </blockquote>
  1447. <blockquote>
  1448. <p>
  1449. Debugging can be so much easier when using user-defined functions much of the time.
  1450. When I was writing a Web spider at one point, I found that $a[URI]s weren&apos;t being handled correctly, and it was crashing my spider.
  1451. I tracked the issue back to my own function, which blindly depended on a built-in function to do some of the parsing.
  1452. As it turned out, the built-in function was broken.
  1453. I ended up having to write a suite of functions for parsing $a[URI]s correctly myself.
  1454. All I had to do was replace the call to the bad function within my own function with my new, good function, and everything worked.
  1455. I didn&apos;t need to find every instance of the bad function getting called, as it&apos;d been only used within my own function as part of a subroutine!
  1456. </p>
  1457. <p>
  1458. I later replaced that whole suite of functions with a class, but in either case, fixing the subroutine fixes the entire script.
  1459. You don&apos;t have to find and fix the same exact bug five times in your code.
  1460. </p>
  1461. </blockquote>
  1462. <blockquote>
  1463. <p>
  1464. You make an excellent point about readability, maintainability, debugability, and scalability.
  1465. Without functions, code can get pretty messy and unmanageable.
  1466. </p>
  1467. <p>
  1468. Using return values from one function from within another function can be useful much of the time as well.
  1469. Two (or more) functions might have sections that can be generalized into a third function to avoid redundancy in the code, the the return value of this third function will usually be useful to the first two functions.
  1470. </p>
  1471. </blockquote>
  1472. <h2 id="Unit7">Unit 7</h2>
  1473. <p>
  1474. The learning guide has a couple mistakes in it.
  1475. First, it says we learned about lists in <a href="https://y.st./en/coursework/CS1101/#Unit5">Unit 5</a>.
  1476. I&apos;m almost certain we didn&apos;t cover lists until <strong>*this*</strong> unit, <a href="https://y.st./en/coursework/CS1101/#Unit7">Unit 7</a>.
  1477. Second, it says that lists can only contain elements of the same type.
  1478. However, just as the textbook says, lists can contain a mix of data types.
  1479. </p>
  1480. <p>
  1481. The reading assignment said that Python doesn&apos;t handle letter comparison the same way people do.
  1482. That&apos;s a bit misleading.
  1483. Python doesn&apos;t handle letter comparison the same way <strong>*all*</strong> people do, but as different people handle letter comparisons differently, Python compares letters exactly like <strong>*some*</strong> people do.
  1484. The simple fact is, upper-case wye (&quot;Y&quot;) and lower-case wye (&quot;y&quot;) are not the same letter.
  1485. We can show this pretty easily with proper nouns and sentence beginnings.
  1486. For example, if your name is &quot;Sam&quot;, is &quot;sam&quot; a proper spelling of your name?
  1487. No.
  1488. It must be a misspelling, then; the wrong letter was used.
  1489. The name must begin in an upper-case letter; upper-case and lower-case letters are <strong>*not*</strong> the same letters!
  1490. </p>
  1491. <p>
  1492. Personally, I don&apos;t care what order is used for alphabetizing as long as it&apos;s easy to understand and is consistent.
  1493. For example, maybe you prefer to alphabetize like this:
  1494. </p>
  1495. <ul>
  1496. <li>
  1497. A
  1498. </li>
  1499. <li>
  1500. a
  1501. </li>
  1502. <li>
  1503. B
  1504. </li>
  1505. <li>
  1506. b
  1507. </li>
  1508. <li>
  1509. C
  1510. </li>
  1511. <li>
  1512. c
  1513. </li>
  1514. </ul>
  1515. <p>
  1516. Or perhaps, you prefer to alphabetize like this:
  1517. </p>
  1518. <ul>
  1519. <li>
  1520. A
  1521. </li>
  1522. <li>
  1523. B
  1524. </li>
  1525. <li>
  1526. C
  1527. </li>
  1528. <li>
  1529. a
  1530. </li>
  1531. <li>
  1532. b
  1533. </li>
  1534. <li>
  1535. c
  1536. </li>
  1537. </ul>
  1538. <p>
  1539. However, I don&apos;t think that this should in any way be considered valid:
  1540. </p>
  1541. <ul>
  1542. <li>
  1543. Aa
  1544. </li>
  1545. <li>
  1546. ab
  1547. </li>
  1548. <li>
  1549. Ac
  1550. </li>
  1551. </ul>
  1552. <p>
  1553. The problem is that it treats &quot;A&quot; and &quot;a&quot; as the same letter, even though they&apos;re not.
  1554. On principle, this is wrong.
  1555. However, it can be bad in practice too.
  1556. For example, lets say you need to keep things in a consistent and predictable order.
  1557. Several people will be handling the alphabetized items, and they all need to be following the same rules for sorting as to keep things in order.
  1558. Let&apos;s say you have some children&apos;s books titled &quot;Happy Banana&quot; and &quot;Happy banana&quot;.
  1559. How do you sort &quot;Banana&quot; and &quot;banana&quot;?
  1560. One way is to simply say the order doesn&apos;t matter.
  1561. This violates the need to keep a consistent order though.
  1562. The other way is to add needlessly-complex sorting rules.
  1563. For example, after a case-insensitive sorting, if two items belong in the same place, you&apos;d sort them case sensitively next or something.
  1564. It removes the ambiguity, but it makes sorting more painful than it needs to be.
  1565. </p>
  1566. <p>
  1567. I had to change desktop interfaces just to escape a stupid sorting problem like this.
  1568. It wasn&apos;t exactly a case sensitivity issue, but file names weren&apos;t being sorted properly and it make it difficult to find files.
  1569. Forcing bizarre sorting algorithms on the user (for example, case-insensitive name comparisons) isn&apos;t a nice thing to do.
  1570. Providing it as an option if fine, and setting it as the default option might even be considered by many to be acceptable or even positive, but at least provide a way to opt out.
  1571. I had several numerically-named files that I&apos;d downloaded a while back.
  1572. My file manager (which was heavily built into the desktop) combined all numeric characters, then sorted by which number was greater.
  1573. I knew the first few digits of the file names, but not the whole number.
  1574. Due to the bizarre sorting, I couldn&apos;t find my files!
  1575. There wasn&apos;t a way to get the file manager to stop treating digit characters as magically special either, and as I couldn&apos;t replace only the file manager because it was too heavily integrated into the desktop, I had to replace the whole desktop.
  1576. Please.
  1577. If you&apos;re building user-facing software that sorts something, files or otherwise, don&apos;t force complex sorting algorithms on the user!
  1578. If you want to implement such algorithms, at least provide a basic $a[ASCII]/Unicode sort as an option for users to switch to if needed!
  1579. This is an extreme example for sure, but even a case-insensitive sort can be mildly irritating to those of use that prefer to sort things more consistently and more reasonably.
  1580. </p>
  1581. <p>
  1582. Lists in Python have very different properties than I&apos;m used to in $a[PHP].
  1583. In $a[PHP], array elements have both a key and a value.
  1584. The key is used to indicate which element you want to access.
  1585. However, Python lists don&apos;t have keys; they have indices.
  1586. This is an important distinction.
  1587. In $a[PHP], if you have an array of five items and you delete the item with key <code>2</code>, you now have an array with keys <code>0</code>, <code>1</code>, <code>3</code>, and <code>4</code>.
  1588. The keys don&apos;t change just because there&apos;s a gap.
  1589. However, Python doesn&apos;t use keys for lists, and instead, elements are accessed via their indices.
  1590. This causes a couple of interesting things to happen when working with lists.
  1591. First, if you delete list elements, the other elements closer to the end appear to &quot;slide over&quot;.
  1592. In the example above of deleting element <code>2</code>, in Python, element <code>3</code> would become the new element <code>2</code> and element <code>4</code> would become the new element <code>3</code>.
  1593. There&apos;d no longer be an element <code>4</code>, but there&apos;s still be an element <code>2</code>.
  1594. Second, negative indices can be used to access items based on position from the end of the list instead of from the beginning.
  1595. Because $a[PHP] uses keys instead of indices, it doesn&apos;t work that way in $a[PHP].
  1596. An key can be negative if you chose it to be, but that negative key won&apos;t be the same as a different positive key.
  1597. </p>
  1598. <p>
  1599. This week&apos;s programming assignment involves writing code to sort the fruit names in a file we&apos;re given.
  1600. The line endings in this file are bizarre though. Instead of being <code>\\n</code> like they should be or <code>\\r\\n</code> like Windows would format them, some of them are <code>\\r\\n\\n</code> and some are <code>\\n\\r\\n</code>, aside from the end on the final line, which is the basic <code>\\n</code>.
  1601. Who wrote this file and what kind of bizarre setup did their computer have?
  1602. Why aren&apos;t their line endings consistent?
  1603. We haven&apos;t covered the cleaning up of messy data such as this, so I&apos;m curious to see how the other students deal with the situation.
  1604. As for me, I&apos;ve coded my <a href="https://y.st./en/coursework/CS1101/sort_fruits.py.xhtml">script</a> to convert the line endings to <code>\\n</code>, as they should have been to start with.
  1605. After getting the script working in Python3, I tested again in Python2.
  1606. There were no detectable differences in how the script ran, so no extra effort was needed to make the script compatible with both versions of the interpreter this week.
  1607. </p>
  1608. <p>
  1609. In addition to the textbook chapters assigned for us to read, some optional videos and supplementary reading material was provided:
  1610. </p>
  1611. <ul>
  1612. <li>
  1613. <a href="https://www.youtube.com/watch?v=9LgyKiq_hU0">For Loops in Python</a>
  1614. </li>
  1615. <li>
  1616. <a href="https://www.youtube.com/watch?v=D0Nb2Fs3Q8c">While Loops in Python</a>
  1617. </li>
  1618. <li>
  1619. <a href="https://www.youtube.com/watch?v=JA4neapsdqQ">Unit7</a>
  1620. </li>
  1621. <li>
  1622. <a href="https://www.youtube.com/watch?v=zEyEC34MY1A">Python Lists</a>
  1623. </li>
  1624. <li>
  1625. <a href="http://effbot.org/zone/python-list.htm">An Introduction to Python Lists</a>
  1626. </li>
  1627. <li>
  1628. <a href="http://www.dreamincode.net/forums/topic/198139-reading-and-writing-to-a-txt-file-in-python/">Attention Required! | Cloudflare</a>
  1629. </li>
  1630. </ul>
  1631. <p>
  1632. Unfortunately, due to poor use of one of my two days off this week, I didn&apos;t have time to go over the supplementary material.
  1633. I did watch the Unit7 video though, and it seems our professor gives us the solution to the assignment this week!
  1634. Or rather, <strong>*a*</strong> solution.
  1635. My submission for the week is structured differently, but it does mostly the same thing.
  1636. I also skipped the turtle exercises from Unit 5.
  1637. I was planning to come back to them, but there&apos;s simply no time to decipher how turtles work right now.
  1638. Other than that, I finished catching up with the exercises from past units.
  1639. </p>
  1640. <p>
  1641. Lastly for the week, I took the ungraded quiz and graded the assignment submissions from last week.
  1642. The first submission I graded was programmed to continue taking input as long as the user wanted to compare numbers.
  1643. After making a comparison, the script asked the user if another comparison was wanted, then looped back to the beginning if the user said yes.
  1644. Why didn&apos;t I think of that!
  1645. I hard-coded my solution to make three comparisons, as three comparisons were needed for the assignment.
  1646. Now I feel foolish.
  1647. The second submission I graded was structured in what I thought was a needlessly-complex way, but it too looped for as long as the user wanted it to!
  1648. Am I just the dunce of the class, unable to think to implement such a simple concept?
  1649. As for the third and final submission I graded, not only was the looping behavior present in the code, it was even present in the flowchart, unlike in the previous two submissions!
  1650. Yup.
  1651. It seems that last week, I was the dunce.
  1652. I don&apos;t think this week&apos;s assignment has the room for creativity that last week&apos;s did though, so I shouldn&apos;t do poorly this time.
  1653. I implemented the line ending normalization, which really, I think&apos;s the only thing the assignment didn&apos;t ask for that fits the goal of the code.
  1654. </p>
  1655. <p>
  1656. My final thought for the week is that I can&apos;t believe I didn&apos;t think to implement a basic, continuous loop.
  1657. I used a loop that was predetermined to execute thrice instead.
  1658. I knew at the time that it was a bad implementation in general, but I thought that given the exercise, it was the best implementation to accomplish the goal.
  1659. I need to generalize better.
  1660. While I need to meet the goals of the assignment, I also need to write code using paradigms consistent with user-facing code.
  1661. I mean, it&apos;s obvious that most of the code we write for this class is not to be run later.
  1662. It&apos;s basic exercises meant to illustrate concepts, so it&apos;s not exactly code you mass distribute and hope other people take interest in.
  1663. However, that doesn&apos;t mean that it shouldn&apos;t meet with certain minimum-quality requirements.
  1664. After all, this is practice; practicing good quality helps one create good quality once it actually matters, too.
  1665. I approach these assignments with fun as one of the goals, but I still take them very seriously.
  1666. </p>
  1667. <h3>Exercises</h3>
  1668. <h4>From <a href="https://y.st./en/coursework/CS1101/#Unit4">Unit 4</a></h4>
  1669. <h5>Exercise 3.3</h5>
  1670. <blockquote>
  1671. <pre><code>def right_justify(string):
  1672. # If right-justification puts the end of the text at column 70, there
  1673. # must be 72 columns total: column 0 is at the beginning and column 71
  1674. # is a line feed.
  1675. indent = 71 - len(string)
  1676. # We don&apos;t want the script to crash, so let&apos;s make sure we don&apos;t try to
  1677. # multiply the space character by a negative number.
  1678. if indent &lt; 0:
  1679. indent = 0
  1680. print(&quot; &quot; * indent + string)</code></pre>
  1681. </blockquote>
  1682. <h5>Exercise 3.4</h5>
  1683. <blockquote>
  1684. <pre><code>def do_twice(f, arg):
  1685. f(arg)
  1686. f(arg)
  1687. def print_twice(arg):
  1688. print(arg)
  1689. print(arg)
  1690. def do_four(f, arg):
  1691. do_twice(f, arg)
  1692. do_twice(f, arg)</code></pre>
  1693. </blockquote>
  1694. <h5>Exercise 3.5</h5>
  1695. <blockquote>
  1696. <pre><code>def grid(size):
  1697. if size &lt; 0:
  1698. size = 0
  1699. for _ in range(size):
  1700. for _ in range(size):
  1701. print(&quot;+ - - - - &quot;, end=&quot;&quot;)
  1702. print(&quot;+&quot;)
  1703. for _ in range(4):
  1704. for _ in range(size):
  1705. print(&quot;| &quot;, end=&quot;&quot;)
  1706. print(&quot;|&quot;)
  1707. for _ in range(size):
  1708. print(&quot;+ - - - - &quot;, end=&quot;&quot;)
  1709. print(&quot;+&quot;)</code></pre>
  1710. </blockquote>
  1711. <h4>From <a href="https://y.st./en/coursework/CS1101/#Unit5">Unit 5</a></h4>
  1712. <h5>Exercise 5.2</h5>
  1713. <blockquote>
  1714. <pre><code>def is_triangle(a, b, c):
  1715. if a &gt; b + c or b &gt; a + c or c &gt; a + b:
  1716. print(&quot;No&quot;)
  1717. else:
  1718. print(&quot;Yes&quot;)
  1719. def prompt_is_triangle():
  1720. a = int(input(&quot;Please enter a value for A.\\n&gt;&gt;&gt; &quot;))
  1721. b = int(input(&quot;Please enter a value for B.\\n&gt;&gt;&gt; &quot;))
  1722. c = int(input(&quot;Please enter a value for C.\\n&gt;&gt;&gt; &quot;))
  1723. is_triangle(a, b, c)</code></pre>
  1724. </blockquote>
  1725. <h4>From <a href="https://y.st./en/coursework/CS1101/#Unit5">Unit 7</a></h4>
  1726. <h5>Exercise 8.1</h5>
  1727. <blockquote>
  1728. <pre><code>def function(string):
  1729. index = len(string)
  1730. while index:
  1731. index -= 1
  1732. print(string[index])</code></pre>
  1733. </blockquote>
  1734. <h5>Exercise 8.2</h5>
  1735. <blockquote>
  1736. <pre><code>prefixes = &quot;JKLMNOPQ&quot;
  1737. suffix = &quot;ack&quot;
  1738. for letter in prefixes:
  1739. if letter == &quot;O&quot; or letter == &quot;Q&quot;:
  1740. letter += &quot;u&quot;
  1741. print(letter + suffix)</code></pre>
  1742. </blockquote>
  1743. <h5>Exercise 8.3</h5>
  1744. <p>
  1745. <code>[:]</code> can simply be omitted from <code>fruit[:]</code>, as it refers to the whole string; the same thing <code>fruit</code> alone refers to.
  1746. </p>
  1747. <h5>Exercise 8.4</h5>
  1748. <blockquote>
  1749. <pre><code>def find(word, letter, index):
  1750. while index &lt; len(word):
  1751. if word[index] == letter:
  1752. return index
  1753. index = index + 1
  1754. return -1</code></pre>
  1755. </blockquote>
  1756. <h5>Exercise 8.5</h5>
  1757. <blockquote>
  1758. <pre><code>def count(word, search):
  1759. count = 0
  1760. for letter in word:
  1761. if letter == search:
  1762. count = count + 1
  1763. print(count)</code></pre>
  1764. </blockquote>
  1765. <h5>Exercise 8.6</h5>
  1766. <blockquote>
  1767. <pre><code>def count(word, search):
  1768. count = 0
  1769. index = find(word, search, 0)
  1770. while index != -1:
  1771. count += 1
  1772. index = find(word, search, index+1)
  1773. print(count)</code></pre>
  1774. </blockquote>
  1775. <h5>Exercise 8.7</h5>
  1776. <blockquote>
  1777. <p>
  1778. <code>&quot;banana&quot;.count(&quot;a&quot;)</code>
  1779. </p>
  1780. </blockquote>
  1781. <h5>Exercise 8.8</h5>
  1782. <blockquote>
  1783. <pre><code>def is_reverse(word1, word2):
  1784. if len(word1) != len(word2):
  1785. return False
  1786. i = 0
  1787. j = len(word2) - 1
  1788. while j &gt; -1:
  1789. print(i, j)
  1790. if word1[i] != word2[j]:
  1791. return False
  1792. i = i+1
  1793. j = j-1
  1794. return True</code></pre>
  1795. </blockquote>
  1796. <h5>Exercise 8.9</h5>
  1797. <blockquote>
  1798. <pre><code>def is_palindrome(string):
  1799. return string == string[::-1]</code></pre>
  1800. </blockquote>
  1801. <h5>Exercise 8.11</h5>
  1802. <p>
  1803. The first function tells only whether the first character in the string is lower-case.
  1804. </p>
  1805. <p>
  1806. The second function returns strings, not boolean values.
  1807. </p>
  1808. <p>
  1809. The third function only tells whether the final character in the string is lower-case.
  1810. As a bonus, this function also throws an exception if the argument provided to it is the empty string.
  1811. </p>
  1812. <p>
  1813. I think the fourth function might actually work as intended.
  1814. </p>
  1815. <p>
  1816. The fifth function tells whether <strong>*all*</strong> the letters in the string are lower-case.
  1817. </p>
  1818. <h5>Exercise 8.12</h5>
  1819. <blockquote>
  1820. <pre><code>def rotate_word(string, rot):
  1821. A = ord(&quot;A&quot;)
  1822. Z = ord(&quot;Z&quot;)
  1823. a = ord(&quot;a&quot;)
  1824. z = ord(&quot;z&quot;)
  1825. return_string = &apos;&apos;
  1826. for char in string:
  1827. integer = ord(char)
  1828. if integer &gt;= A and integer &lt;= Z:
  1829. # It&apos;s easy to say that if the resulting addition from applying the
  1830. # rotation to the letter is too big, to simply subtract twenty-six.
  1831. # However, that doesn&apos;t work in practice because of two corner cases.
  1832. # First, the easiest way to decode the rotation is to rotate by a
  1833. # negative version of the same amount used to encode. Using a basic
  1834. # subtraction, some characters might dip below &quot;A&quot; and become
  1835. # non-letter characters. That&apos;s not what we want. We *could* account
  1836. # for that by checking the lower bounds as well, but there&apos;s no point
  1837. # because of the second corner case. Fixing the second corner case
  1838. # fixes the first without further effort.
  1839. #
  1840. # The second corner case is more interesting: integers with a high
  1841. # absolute value. What happens if you rotate by, say, fifty or negative
  1842. # fifty? If we check the upper and lower bounds after applying the
  1843. # rotation, then simply add or subtract once to loop around, we&apos;ll
  1844. # still be out of bounds and our function will still provide the wrong
  1845. # return value. There are two ways to fix this. The first is to put the
  1846. # check in a loop and keep looping until we have a valid character
  1847. # value. That seems somehow excessive though. The second option is to
  1848. # use modulo division. Modulo division basically loops around for us,
  1849. # then just provides us with the remainder. That sounds exactly like
  1850. # what we want, aside from that we want an offset. We can simulate the
  1851. # offset through subtracting the lower-bound letter.
  1852. offset = A
  1853. elif integer &gt;= a and integer &lt;= z:
  1854. offset = a
  1855. else:
  1856. return_string += char
  1857. continue
  1858. integer -= offset
  1859. integer += rot
  1860. integer %= 26
  1861. return_string += chr(integer+offset)
  1862. return return_string</code></pre>
  1863. </blockquote>
  1864. <h5>Exercise 10.1</h5>
  1865. <blockquote>
  1866. <pre><code>def cumulative_sum(list):
  1867. r = []
  1868. sum = 0
  1869. for i in list:
  1870. sum += i
  1871. r.append(sum)
  1872. return r</code></pre>
  1873. </blockquote>
  1874. <h5>Exercise 10.2</h5>
  1875. <blockquote>
  1876. <pre><code>def chop(list):
  1877. del list[0]
  1878. del list[-1]
  1879. def middle(list):
  1880. return list[1:-1]</code></pre>
  1881. </blockquote>
  1882. <h5>10.3</h5>
  1883. <blockquote>
  1884. <pre><code>def is_sorted(list):
  1885. for i in range(len(list)-1):
  1886. if list[i] &gt; list[i+1]:
  1887. return False
  1888. return True</code></pre>
  1889. </blockquote>
  1890. <h5>10.4</h5>
  1891. <blockquote>
  1892. <pre><code>def is_anagram(x, y):
  1893. x = list(x)
  1894. x.sort()
  1895. x = &apos;&apos;.join(x)
  1896. y = list(y)
  1897. y.sort()
  1898. y = &apos;&apos;.join(y)
  1899. return x == y</code></pre>
  1900. </blockquote>
  1901. <h5>Exercise 10.5</h5>
  1902. <blockquote>
  1903. <pre><code>def has_duplicates(list):
  1904. test = list[:]
  1905. test.sort()
  1906. for i in range(len(test)-1):
  1907. if test[i] == test[i+1]:
  1908. return True
  1909. return False</code></pre>
  1910. </blockquote>
  1911. <h5>Exercise 10.6</h5>
  1912. <blockquote>
  1913. <pre><code>def remove_duplicates(list):
  1914. r = []
  1915. for i in list:
  1916. if i not in r:
  1917. r.append(i)
  1918. return r</code></pre>
  1919. </blockquote>
  1920. <h5>Exercise 10.7</h5>
  1921. <blockquote>
  1922. <pre><code>def function_0():
  1923. file = open(&quot;words.txt&quot;, &quot;r&quot;)
  1924. list = file.read().split()
  1925. file.close()
  1926. # We should simply return the list, but the exercise wants us to build
  1927. # the return list another way.
  1928. t = []
  1929. for x in list:
  1930. t.append(x)
  1931. return t
  1932. def function_0():
  1933. file = open(&quot;words.txt&quot;, &quot;r&quot;)
  1934. list = file.read().split()
  1935. file.close()
  1936. # We should simply return the list, but the exercise wants us to build
  1937. # the return list another way.
  1938. t = []
  1939. for x in list:
  1940. t = t + [x]
  1941. return t</code></pre>
  1942. </blockquote>
  1943. <h5>Exercise 10.9</h5>
  1944. <blockquote>
  1945. <pre><code>def find_reverse_pairs(list):
  1946. r = []
  1947. for i in list:
  1948. if i[::-1] in list:
  1949. test0 = (i, i[::-1])
  1950. test1 = (i[::-1], i)
  1951. if test0 not in r and test1 not in r:
  1952. r.append(test0)
  1953. return r</code></pre>
  1954. </blockquote>
  1955. <h5>Exercise 10.10</h5>
  1956. <blockquote>
  1957. <p>
  1958. If I understand the instructions correctly, I&apos;d need a list of all valid words in the language to complete this exercise.
  1959. That seems like overkill for a basic exercise, so I&apos;m probably not understanding the instructions.
  1960. </p>
  1961. </blockquote>
  1962. <h5>Exercise 12.1</h5>
  1963. <blockquote>
  1964. <pre><code>def sumall(*args):
  1965. sum = 0
  1966. for i in args:
  1967. sum += i
  1968. return sum</code></pre>
  1969. </blockquote>
  1970. <h5>Exercise 12.2</h5>
  1971. <blockquote>
  1972. <pre><code>import random
  1973. def sort_by_length(words):
  1974. t = []
  1975. for word in words:
  1976. t.append((len(word), random.random(), word))
  1977. t.sort(reverse=True)
  1978. res = []
  1979. for length, rand, word in t:
  1980. res.append(word)
  1981. return res</code></pre>
  1982. </blockquote>
  1983. <h5>Exercise 12.3</h5>
  1984. <blockquote>
  1985. <pre><code>def most_frequent(string):
  1986. count = {}
  1987. for char in string:
  1988. if char not in count:
  1989. count[char] = 0
  1990. count[char] += 1
  1991. count_list = []
  1992. for char in count:
  1993. count_list.append((count[char], char))
  1994. count_list.sort()
  1995. count_list.reverse()
  1996. for tuple in count_list:
  1997. print(tuple[1])</code></pre>
  1998. </blockquote>
  1999. <h3>Exercise 12.4</h3>
  2000. <blockquote>
  2001. <pre><code>def anagrams(list):
  2002. dict = {}
  2003. for word in list:
  2004. key = &quot;&quot;.join(sorted(word))
  2005. if key not in dict:
  2006. dict[key] = []
  2007. dict[key].append(word)
  2008. dict_list = []
  2009. for set in dict:
  2010. dict_list.append((len(dict[set]), dict[set]))
  2011. dict_list.sort()
  2012. dict_list.reverse()
  2013. for set in dict_list:
  2014. print(set[1])</code></pre>
  2015. </blockquote>
  2016. <p>
  2017. The second part of this exercise seems to require processing an entire dictionary.
  2018. That seems like overkill for an exercise.
  2019. </p>
  2020. <h5>Exercise 12.5</h5>
  2021. <p>
  2022. This exercise seems to require a file that wasn&apos;t included in the textbook.
  2023. </p>
  2024. <h3>Discussion board drafts</h3>
  2025. <blockquote>
  2026. <p>
  2027. The main advantage of test-driven development, in my opinion, is the testing suite you have to build to make it work.
  2028. Whenever there&apos;s an accidental regression, you&apos;ll notice it when you rerun your tests (which should be before your next release).
  2029. You should test every case you can think of that might break your code, as well as some normal use cases.
  2030. Once you&apos;ve developed code to test your main code, you simply keep your test code on hand in a separate repository.
  2031. If you update your code and it ever fails your tests, you know one of two things has happened.
  2032. First and most likely, you&apos;ve broken something in your code.
  2033. The testing suite has picked up on the presence of the error, so now all you&apos;ve got to do is find and correct it.
  2034. The second option is that you&apos;ve intentionally altered the output of your code, and the testing suite&apos;s out of date.
  2035. In this case, simply update the testing suite with code that properly handles the new output of your main code.
  2036. Additionally, the testing suite can be updated as bug reports are filed or bugs are found by you.
  2037. Set up the testing suite to test that particular case, then after you fix your code, your code will pass.
  2038. This&apos;ll help prevent regression and reintroduction of this bug.
  2039. </p>
  2040. <p>
  2041. The only disadvantage I can find in test-driven development is the time and effort it takes to build the test suite.
  2042. It&apos;s so much easier to test small parts as you go and debug that way than it is to build actual tests for the output.
  2043. While I fully intend to keep my test suite up to date with all new functionality I add to my code, the truth is that I fall short of that.
  2044. Much of my code still doesn&apos;t have repeatable test cases built for it yet, and likely never will.
  2045. My time is finite, and I barely have time to work on my code at all, let alone build test code that&apos;ll never be deployed outside my own laptop.
  2046. </p>
  2047. </blockquote>
  2048. <blockquote>
  2049. <p>
  2050. Oops, I explained the advantages and disadvantages, but I forgot to actually specify what test-driven development is!
  2051. Test-driven development is a development process that involves building a testing suite alongside the main project you&apos;re developing.
  2052. As you go, you update the testing suite to account for various things the project code might do, alerting you to when it does something incorrectly.
  2053. Even after you&apos;re theoretically done with the testing code, you&apos;re never actually done with it.
  2054. You keep it to run alongside your new tests, so if old functionality breaks, you get alerted to that as well.
  2055. </p>
  2056. </blockquote>
  2057. <blockquote>
  2058. <p>
  2059. Test-writing can be quite time-consuming for sure.
  2060. When you write tests before you write your code, you might also run into the fact that you don&apos;t know what your code&apos;s output will be until you write it.
  2061. I often plan my code to behave one way, but find a better behavior part-way through development.
  2062. In cases such as those, tests written beforehand will fail.
  2063. To be clear, I don&apos;t just mean that the internals will work differently.
  2064. You may find you need different input to your code to make it work, or you might think of more useful output or a more useful format for the output.
  2065. </p>
  2066. </blockquote>
  2067. <blockquote>
  2068. <p>
  2069. I like the wording you used on that.
  2070. The test code chechs to see that actual output matches expected output.
  2071. If it doesn&apos;t, an error&apos;s been introduced somewhere; the error must be found and corrected.
  2072. </p>
  2073. </blockquote>
  2074. <blockquote>
  2075. <p>
  2076. Small cycles can indeed be helpful.
  2077. Taking on too much at once has the possibility of introducing many errors at once.
  2078. With several errors, a longer debugging session would be needed, and the errors would be potentially more difficult to find.
  2079. </p>
  2080. </blockquote>
  2081. <h4>Exercise 11.7</h4>
  2082. <blockquote>
  2083. <pre><code>def has_duplicates(list):
  2084. dict = {}
  2085. for i in list:
  2086. if i in dict:
  2087. return True
  2088. else:
  2089. dict[i] = True
  2090. return False</code></pre>
  2091. </blockquote>
  2092. <h2 id="Unit8">Unit 8</h2>
  2093. <p>
  2094. The learning guide says that in Python, dictionaries are implemented as objects, so by working with them, we&apos;ll get a taste of object-oriented programming.
  2095. Python dictionaries are objects - that&apos;s technically correct.
  2096. WHat it fails to take into account though is that <strong>*everything*</strong> in Python is an object!
  2097. Not literally everything; operators and such aren&apos;t objects.
  2098. However, every variable contains and all nameable structures (for example, functions) are objects stored in variables.
  2099. Even <code>None</code> and the boolean values are objects!
  2100. We&apos;ve been working with objects the whole time, and dictionaries aren&apos;t much different than lists as far as how much they express that they&apos;re objects.
  2101. </p>
  2102. <p>
  2103. The learning guide also says that Windows uses the backslash character as a path separator, which again, is technically correct.
  2104. However, is says that it uses it <strong>*instead*</strong> of the forward slash.
  2105. This is incorrect.
  2106. Most reasonable systems use the forward slash character.
  2107. However, Windows instead uses both the forward slash <strong>*and*</strong> the backslash as path separators.
  2108. In fact, in Windows file paths, the two characters are interchangeable.
  2109. Yeah.
  2110. Windows is <strong>*that*</strong> messed up.
  2111. In any case, what does this mean in practical terms?
  2112. It means that one should <strong>*almost always*</strong> use the forward slash in file paths.
  2113. By doing so, the path will (unless prefixed with a Windows drive letter) work on most operating systems without any negative consequences in Windows.
  2114. Like the learning guide says too, the backslash is used as the escape character in Python and several other languages.
  2115. Using the backslash in a string file path requires using a double backslash instead of a single backslash.
  2116. Using forward slashes is just <strong>*easier*</strong>!
  2117. I&apos;d never advocate ease over doing things right, but when ease lines up with doing things right like this, it makes it difficult to justify doing things the wrong way; there&apos;s simply no excuse for using backslashes in file paths in most cases.
  2118. </p>
  2119. <p>
  2120. The first assignment submission I graded from last week was just awful.
  2121. It didn&apos;t do <strong>*anything*</strong> the assignment told us to have it do!
  2122. It didn&apos;t read an input file, didn&apos;t parse anything, and didn&apos;t write its output to an output file.
  2123. Some people just can&apos;t follow simple directions.
  2124. In fact, the syntax is bad for even what it does try to actually do, which is define an array statically in-file, so it doesn&apos;t even manage to do that.
  2125. I tried running it in both Python2 and Python3 to be sure I was right about the strange-looking syntax, and sure enough, this thing doesn&apos;t even make it past the parsing stage.
  2126. I think it might use ... C syntax?
  2127. I&apos;m not sure, I don&apos;t know C yet.
  2128. The second submission accounted for the strangeness of the line endings in the input file by ... providing a custom input file.
  2129. Really?
  2130. You can&apos;t account for bad input by replacing it with your own specially-crafted input by hand before your script even runs!
  2131. The real kicker though is that the custom input file&apos;s bad too.
  2132. As such, the output of the script is incorrect.
  2133. The final submission used pseudocode to plan the script.
  2134. This pseudocode was commented out too, which was interesting.
  2135. While it wasn&apos;t included in the real script, it <strong>*could*</strong> have been because it&apos;d been commented out.
  2136. Unfortunately, the way the script dealt with the bizarre line endings was to read the whole file into a variable, then use <code>.split()</code> to break the file into words.
  2137. Judging by the provided output, this removed extra line ending characters.
  2138. However, it also broke multi-word fruit names into separate fruit!
  2139. So close to perfect, but not quite.
  2140. </p>
  2141. <p>
  2142. Lastly for the week, I took the ungraded quiz.
  2143. I did better than I thought I would, and actually got the history questions right.
  2144. </p>
  2145. <h3>Epilogue (Unit 9)</h3>
  2146. <p>
  2147. I tried to complete the course evaluations, but it seems I was too late for that.
  2148. This week&apos;s been very busy for me, so I though I&apos;d fill out the evaluations during the slower exam week.
  2149. No such luck, they were due yesterday.
  2150. That really suck, as I had an issue I really wanted to report.
  2151. My professor also recommended retaking the ungraded quizzes before the exam, but those were still closed during Unit 8.
  2152. I thought maybe they&apos;d open up again during Unit 9, but that didn&apos;t happen.
  2153. Too bad, too, I was looking forward to going back any finally taking the <a href="https://y.st./en/coursework/CS1101/#Unit2">Unit 2</a> ungraded quiz I missed.
  2154. </p>
  2155. <p>
  2156. Unit 9, the exam unit, came with its own ungraded quiz, so I took that before the final exam.
  2157. The ungraded quiz was actually both longer and tougher than the actual exam.
  2158. The actual exam didn&apos;t ask any history questions, so I should have scored pretty high on that.
  2159. </p>
  2160. <p>
  2161. My final thoughts for the term are that I really hope my tuition issue gets cleared up in time for next term.
  2162. I sent a cashier&apos;s cheque to University of the People for \$200 $a[USD], which would cover both courses I took this term.
  2163. However, University of the People is now claiming I only paid for <strong>*one*</strong> of my two courses!
  2164. If they don&apos;t fix this, they say they&apos;ll withdraw me from my courses for next term.
  2165. I already hit a snag in the system last term, causing me to only have been able to register for one course that term, so I&apos;m a course behind schedule.
  2166. I don&apos;t want to be another two courses behind!
  2167. What do I do?
  2168. This sucks.
  2169. I&apos;ve written to the billing department, and am waiting for an email back from them as I type this.
  2170. All I can do is hope they get it sorted out.
  2171. </p>
  2172. <h3>Exercises</h3>
  2173. <h4>Exercise 11.1</h4>
  2174. <p>
  2175. Without knowing the format of the word file, this exercise is impossible to complete.
  2176. </p>
  2177. <h4></h4>
  2178. <blockquote>
  2179. <pre><code>def histogram(s):
  2180. d = dict()
  2181. for c in s:
  2182. d[c] = d.get(c, 0) + 1
  2183. return d</code></pre>
  2184. </blockquote>
  2185. <h4>Exercise 11.3</h4>
  2186. <blockquote>
  2187. <pre><code>def print_hist(h):
  2188. for c in h.keys().sort():
  2189. print(c, h[c])</code></pre>
  2190. </blockquote>
  2191. <h4>Exercise 11.4</h4>
  2192. <blockquote>
  2193. <pre><code>def reverse_lookup(d, v):
  2194. list = []
  2195. for k in d:
  2196. if d[k] == v:
  2197. list.append(k)
  2198. list.sort()
  2199. return list</code></pre>
  2200. </blockquote>
  2201. <h4>Exercise 11.5</h4>
  2202. <blockquote>
  2203. <pre><code>def invert_dict(d):
  2204. inv = dict()
  2205. for key in d:
  2206. val = d[key]
  2207. inv.setdefault(val, [])
  2208. inv[val].append(key)
  2209. return inv</code></pre>
  2210. </blockquote>
  2211. <h4>Exercise 11.7</h4>
  2212. <blockquote>
  2213. <pre><code>def has_duplicates(list):
  2214. dict = {}
  2215. for i in list:
  2216. if i in dict:
  2217. return True
  2218. else:
  2219. dict[i] = True
  2220. return False</code></pre>
  2221. </blockquote>
  2222. <h4>Exercise 11.8</h4>
  2223. <blockquote>
  2224. <pre><code>def rotate_pairs(list):
  2225. l = len(list)
  2226. r = []
  2227. for a in range(l):
  2228. for b in range(a+1, l):
  2229. if len(list[a]) == len(list[b]):
  2230. a_ord = ord(list[a][0])
  2231. b_ord = ord(list[b][0])
  2232. # Because we built rotate_word() to handle negative numbers as
  2233. # expected (see the solution to Exercise 8.12 above), we don&apos;t even
  2234. # need to know if the offset is positive or negative. We&apos;ll just pass
  2235. # it into the function without further preprocessing. It pays to do
  2236. # things the right way. You never know when you&apos;ll need to reuse your
  2237. # functions in an unexpected way, so making them handle all
  2238. # technically-valid input in the correct way will save you development
  2239. # time when you later reuse your code.
  2240. offset = b_ord - a_ord
  2241. test = rotate_word(list[a], offset)
  2242. if test == list[b]:
  2243. r.append((list[a], list[b]))
  2244. return r</code></pre>
  2245. </blockquote>
  2246. <h4>Exercise 11.9</h4>
  2247. <p>
  2248. <p>
  2249. Language-processing?
  2250. Seriously?
  2251. That&apos;s a bit extensive for an entry-level course.
  2252. Given my finite time, I&apos;m going to have to skip this.
  2253. </p>
  2254. </p>
  2255. <h4>Exercise 14.1</h4>
  2256. <blockquote>
  2257. <pre><code>def walk(dir):
  2258. list = []
  2259. for name in os.listdir(dir):
  2260. path = os.path.join(dir, name)
  2261. if os.path.isfile(path):
  2262. list.append(path)
  2263. else:
  2264. list += walk(path)
  2265. return list</code></pre>
  2266. </blockquote>
  2267. <h4>Exercise 14.2</h4>
  2268. <blockquote>
  2269. <pre><code>def print_files(directory)
  2270. unparsed = os.walk(directory)
  2271. parsed = []
  2272. i = 0
  2273. for i in unparsed:
  2274. dir = i[0]
  2275. for file in i[2]:
  2276. parsed.append(dir+&quot;/&quot;+file)
  2277. parsed.sort()
  2278. for i in parsed:
  2279. print(i)</code></pre>
  2280. </blockquote>
  2281. <h4>Exercise 14.3</h4>
  2282. <blockquote>
  2283. <pre><code>import dbm
  2284. import pickle
  2285. def anagrams(list):
  2286. dict = {}
  2287. db = dbm.open(&quot;anagrams.db&quot;, &quot;c&quot;)
  2288. for word in list:
  2289. key = &quot;&quot;.join(sorted(word))
  2290. if key not in dict:
  2291. dict[key] = []
  2292. dict[key].append(word)
  2293. for set in dict:
  2294. db[set] = pickle.dumps(dict[set])
  2295. db.close()
  2296. def display_anagrams():
  2297. db = dbm.open(&quot;anagrams.db&quot;, &quot;c&quot;)
  2298. dict_list = []
  2299. for set in db:
  2300. dict_list.append((len(db[set]), picke.loads(db[set])))
  2301. dict_list.sort()
  2302. dict_list.reverse()
  2303. for set in dict_list:
  2304. print(set[1])
  2305. db.close()</code></pre>
  2306. </blockquote>
  2307. <h4>Exercise 14.5</h4>
  2308. <p>
  2309. This exercise requires network access.
  2310. We haven&apos;t covered how to use SOCKS proxies in Python yet, and I never go online without tunneling my traffic through $a[Tor].
  2311. Normally, this is a choice I make out of principle.
  2312. However, for the time being, I don&apos;t have home Internet access and my only way to get online is by using a proxy on my mobile device (I can&apos;t seem to get tethering to function).
  2313. The only two on-device proxies I&apos;ve spotted both tunnel traffic through $a[Tor], so $a[Tor]&apos;s my only gateway to the Internet for the time being.
  2314. </p>
  2315. <h4>Exercise 14.6</h4>
  2316. <blockquote>
  2317. <pre><code>import os
  2318. def list_files(directory, extension):
  2319. index = 0 - len(extension)
  2320. unparsed = os.walk(directory)
  2321. parsed = []
  2322. i = 0
  2323. for i in unparsed:
  2324. dir = i[0]
  2325. for file in i[2]:
  2326. if file[index:] == extension:
  2327. parsed.append(dir+&quot;/&quot;+file)
  2328. parsed.sort()
  2329. return parsed
  2330. def find_duplicates(directory, extension):
  2331. dupes = []
  2332. dict = {}
  2333. for file in files:
  2334. c = os.popen(&quot;md5sum &quot;+file)
  2335. md5sum = c.read().split()[0]
  2336. c.close()
  2337. if md5sum in dict:
  2338. dupes.append((dict[md5sum], file))
  2339. else:
  2340. dict[md5sum] = file
  2341. return dupes</code></pre>
  2342. </blockquote>
  2343. <h3>Discussion post drafts</h3>
  2344. <blockquote>
  2345. <p>
  2346. A Python dictionary is basically an unordered list in which keys are used instead of numeric indices.
  2347. Both dictionaries and lists provide something the other does not.
  2348. Lists provide order, while dictionaries provide meaningful keys.
  2349. My native language is $a[PHP], so I find this separation to be very cumbersome.
  2350. $a[PHP] provides ordered key/value pairs.
  2351. In $a[PHP], you don&apos;t have to choose between meaningful indices and a dependable order, but in Python, you do.
  2352. </p>
  2353. <p>
  2354. In Python, dictionaries can be used any time the order of your values doesn&apos;t matter, but the names of your keys does.
  2355. For example, I used a dictionary in the solution to one of the exercises from the book this week:
  2356. </p>
  2357. <blockquote>
  2358. <pre><code>import os
  2359. def list_files(directory, extension):
  2360. index = 0 - len(extension)
  2361. unparsed = os.walk(directory)
  2362. parsed = []
  2363. i = 0
  2364. for i in unparsed:
  2365. dir = i[0]
  2366. for file in i[2]:
  2367. if file[index:] == extension:
  2368. parsed.append(dir+&quot;/&quot;+file)
  2369. parsed.sort()
  2370. return parsed
  2371. def find_duplicates(directory, extension):
  2372. dupes = []
  2373. dict = {}
  2374. for file in files:
  2375. c = os.popen(&quot;md5sum &quot;+file)
  2376. md5sum = c.read().split()[0]
  2377. c.close()
  2378. if md5sum in dict:
  2379. dupes.append((dict[md5sum], file))
  2380. else:
  2381. dict[md5sum] = file
  2382. return dupes</code></pre>
  2383. </blockquote>
  2384. <p>
  2385. Here, the dictionary&apos;s keys matter, but the order of the keys doesn&apos;t.
  2386. The keys are $a[MD5] hashes of known files; we need to know if we&apos;ve seen a particular hash before, but we don&apos;t need to know at what point we say it.
  2387. Instead, we only need to know <strong>*where*</strong> we saw it, so the values are the file names.
  2388. </p>
  2389. </blockquote>
  2390. <blockquote>
  2391. <p>
  2392. I know, right?
  2393. I&apos;m all in favor of properly indenting code, but I usually don&apos;t indent my scaffolding.
  2394. If you don&apos;t indent it, it&apos;s easier to find and remove before releasing the code.
  2395. However, that trick doesn&apos;t work in Python, as Python uses whitespace as a form of syntax, a very strange concept.
  2396. </p>
  2397. </blockquote>
  2398. <blockquote>
  2399. <p>
  2400. I noticed you used $a[DNS] names (<code>com</code>, <code>com.uk</code>, <code>fr</code>, <code>ru</code>) to denote different countries, which is a good idea.
  2401. However, <code>com</code> isn&apos;t really specific to the United States and the &quot;com.&quot; isn&apos;t really necessary in <code>com.uk</code> when just identifying the country.
  2402. Every country actually has its own country code, which is the basis of $a[ccTLD]s (those two-letter $a[TLD]s such as <code>uk</code>, <code>fr</code>, and <code>ru</code>).
  2403. All major countries (and some non-countries) have one.
  2404. I think all the smaller countries have one too, but I can&apos;t swear to that.
  2405. For example, the $a[ccTLD] of the United States isn&apos;t <code>com</code>, but <code>us</code>.
  2406. It&apos;s not as widely-used in the United States as it could be, but several United-States-based websites use it.
  2407. <a href="https://www.replicant.us/"><code>https://www.replicant.us/</code></a> for example has a domain ending in <code>.us</code>.
  2408. </p>
  2409. <p>
  2410. I&apos;m sorry that that got a bit off-topic, but <a href="https://y.st./en/URI_research/ccTLDs.xhtml">$a[ccTLD]s are something I&apos;ve researched throughly in my search for a short domain name</a>.
  2411. </p>
  2412. </blockquote>
  2413. <blockquote>
  2414. <p>
  2415. Any value can be used as a dictionary value, you&apos;re right.
  2416. It&apos;s also worth noting though that only <strong>*immutable*</strong> values can be used as the keys.
  2417. Immutable keys are mappable to mutable or immutable values.
  2418. It&apos;s strange to me that Python dictionaries are unordered.
  2419. In every other context I&apos;ve worth with, values have always had an order to them.
  2420. </p>
  2421. </blockquote>
  2422. <blockquote>
  2423. <p>
  2424. You make a good point about speed.
  2425. Checking the value of every list item isn&apos;t fast, so dictionaries are a good option for when many queries on the data will be needed, as long as those queries can be made using keys and not the values themselves.
  2426. Key-value pairs are a powerful construct and Python dictionaries use them well.
  2427. </p>
  2428. <p>
  2429. I like your example, too.
  2430. It shows how dictionaries can even have dictionaries as their values.
  2431. </p>
  2432. </blockquote>
  2433. END
  2434. );