Attachment 'lec6.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3 Created on Wed Sep 21 11:52:34 2016
   4 
   5 @author: WELG
   6 """
   7 
   8 #####################################
   9 # EXAMPLE:  Towers of Hanoi
  10 #####################################
  11 
  12 def printMove(fr, to):
  13     print('move from ' + str(fr) + ' to ' + str(to))
  14 
  15 def Towers(n, fr, to, spare):
  16     if n == 1:
  17         printMove(fr, to)
  18     else:
  19         Towers(n-1, fr, spare, to)
  20         Towers(1, fr, to, spare)
  21         Towers(n-1, spare, to, fr)
  22 
  23 #print(Towers(4, 'P1', 'P2', 'P3'))
  24 
  25 #####################################
  26 # EXAMPLE:  fibonacci
  27 #####################################
  28 
  29 def fib(x):
  30     """assumes x an int >= 0
  31        returns Fibonacci of x"""
  32     if x == 0 or x == 1:
  33         return 1
  34     else:
  35         return fib(x-1) + fib(x-2)
  36         
  37 #####################################
  38 # EXAMPLE:  testing for palindromes
  39 #####################################
  40         
  41 def isPalindrome(s):
  42 
  43     def toChars(s):
  44         s = s.lower()
  45         ans = ''
  46         for c in s:
  47             if c in 'abcdefghijklmnopqrstuvwxyz':
  48                 ans = ans + c
  49         return ans
  50 
  51     def isPal(s):
  52         if len(s) <= 1:
  53             return True
  54         else:
  55             return s[0] == s[-1] and isPal(s[1:-1])
  56 
  57     return isPal(toChars(s))
  58 
  59 #print(isPalindrome('eve'))
  60 #
  61 #print(isPalindrome('Able was I, ere I saw Elba'))
  62 #
  63 #print(isPalindrome('Is this a palindrome'))
  64 
  65 #####################################
  66 # EXAMPLE: using dictionaries
  67 #          counting frequencies of words in song lyrics
  68 #####################################
  69 
  70 def lyrics_to_frequencies(lyrics):
  71     myDict = {}
  72     for word in lyrics:
  73         if word in myDict:
  74             myDict[word] += 1
  75         else:
  76             myDict[word] = 1
  77     return myDict
  78     
  79     
  80 she_loves_you = ['she', 'loves', 'you', 'yeah', 'yeah', 
  81 'yeah','she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
  82 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
  83 
  84 'you', 'think', "you've", 'lost', 'your', 'love',
  85 'well', 'i', 'saw', 'her', 'yesterday-yi-yay',
  86 "it's", 'you', "she's", 'thinking', 'of',
  87 'and', 'she', 'told', 'me', 'what', 'to', 'say-yi-yay',
  88 
  89 'she', 'says', 'she', 'loves', 'you',
  90 'and', 'you', 'know', 'that', "can't", 'be', 'bad',
  91 'yes', 'she', 'loves', 'you',
  92 'and', 'you', 'know', 'you', 'should', 'be', 'glad',
  93 
  94 'she', 'said', 'you', 'hurt', 'her', 'so',
  95 'she', 'almost', 'lost', 'her', 'mind',
  96 'and', 'now', 'she', 'says', 'she', 'knows',
  97 "you're", 'not', 'the', 'hurting', 'kind',
  98 
  99 'she', 'says', 'she', 'loves', 'you',
 100 'and', 'you', 'know', 'that', "can't", 'be', 'bad',
 101 'yes', 'she', 'loves', 'you',
 102 'and', 'you', 'know', 'you', 'should', 'be', 'glad',
 103 
 104 'oo', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
 105 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
 106 'with', 'a', 'love', 'like', 'that',
 107 'you', 'know', 'you', 'should', 'be', 'glad',
 108 
 109 'you', 'know', "it's", 'up', 'to', 'you',
 110 'i', 'think', "it's", 'only', 'fair',
 111 'pride', 'can', 'hurt', 'you', 'too',
 112 'pologize', 'to', 'her',
 113 
 114 'Because', 'she', 'loves', 'you',
 115 'and', 'you', 'know', 'that', "can't", 'be', 'bad',
 116 'Yes', 'she', 'loves', 'you',
 117 'and', 'you', 'know', 'you', 'should', 'be', 'glad',
 118 
 119 'oo', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
 120 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
 121 'with', 'a', 'love', 'like', 'that',
 122 'you', 'know', 'you', 'should', 'be', 'glad',
 123 'with', 'a', 'love', 'like', 'that',
 124 'you', 'know', 'you', 'should', 'be', 'glad',
 125 'with', 'a', 'love', 'like', 'that',
 126 'you', 'know', 'you', 'should', 'be', 'glad',
 127 'yeah', 'yeah', 'yeah',
 128 'yeah', 'yeah', 'yeah', 'yeah'
 129 ]
 130 
 131 beatles = lyrics_to_frequencies(she_loves_you)
 132 
 133 
 134 def most_common_words(freqs):
 135     values = freqs.values()
 136     best = max(freqs.values())
 137     words = []
 138     for k in freqs:
 139         if freqs[k] == best:
 140             words.append(k)
 141     return (words, best)
 142     
 143 def words_often(freqs, minTimes):
 144     result = []
 145     done = False
 146     while not done:
 147         temp = most_common_words(freqs)
 148         if temp[1] >= minTimes:
 149             result.append(temp)
 150             for w in temp[0]:
 151                 del(freqs[w])  #remove word from dict
 152         else:
 153             done = True
 154     return result
 155     
 156 #print(words_often(beatles, 5))
 157 
 158 #####################################
 159 # EXAMPLE: comparing fibonacci using memoization
 160 #####################################
 161 
 162 
 163 def fib(n):
 164     if n == 1:
 165         return 1
 166     elif n == 2:
 167         return 2
 168     else:
 169         return fib(n-1) + fib(n-2)
 170 
 171 
 172 def fib_efficient(n, d):
 173     if n in d:
 174         return d[n]
 175     else:
 176         ans = fib_efficient(n-1, d)+fib_efficient(n-2, d)
 177         d[n] = ans
 178         return ans
 179         
 180 d = {1:1, 2:2}
 181 
 182 argToUse = 34
 183 #print("")
 184 #print('using fib')
 185 #print(fib(argToUse))
 186 #print("")
 187 #print('using fib_efficient')
 188 #print(fib_efficient(argToUse, d))

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2017-07-27 19:18:45, 142.4 KB) [[attachment:Acetaminophen_PBPK.cps]]
  • [get | view] (2017-07-31 17:20:41, 1449.3 KB) [[attachment:Andy RK.zip]]
  • [get | view] (2017-07-27 19:18:53, 59.4 KB) [[attachment:BIOMD0000000619.xml]]
  • [get | view] (2017-08-09 16:50:30, 13731.9 KB) [[attachment:Belmonte_thesis.pdf]]
  • [get | view] (2017-07-27 19:33:15, 98.1 KB) [[attachment:Bloomington_and_WoodburnHall.pdf]]
  • [get | view] (2017-08-02 19:58:08, 69593.2 KB) [[attachment:CC3D Intro.pptx]]
  • [get | view] (2017-07-31 16:33:05, 1585.0 KB) [[attachment:CC3D-Computational-TB-Ruggiero.pptx]]
  • [get | view] (2017-07-31 13:39:48, 110507.5 KB) [[attachment:CC3D-Lecture Morning 1 August 2017v4-1.pptx]]
  • [get | view] (2017-07-27 15:39:09, 91.8 KB) [[attachment:CC3D_Quick_Reference_Guide.pdf]]
  • [get | view] (2017-07-27 16:26:46, 43.9 KB) [[attachment:CC3D_card.png]]
  • [get | view] (2017-08-04 21:45:14, 189.5 KB) [[attachment:CancerFLowchart.bmp]]
  • [get | view] (2017-08-04 21:38:26, 180.5 KB) [[attachment:CancerFlowCgart.pdf]]
  • [get | view] (2017-08-04 17:24:52, 5066.1 KB) [[attachment:CellCompartmentsAndLinks.pptx]]
  • [get | view] (2017-08-01 16:43:08, 27.8 KB) [[attachment:Chaotic Lorenz in COPASI.cps]]
  • [get | view] (2017-07-27 19:19:01, 1.1 KB) [[attachment:Critchley_human_APAP_data.csv]]
  • [get | view] (2017-08-02 14:44:17, 550.8 KB) [[attachment:Defining ODEs in COPASI--SBML.pptx]]
  • [get | view] (2017-07-27 19:33:23, 461.2 KB) [[attachment:Detailed_IU_map.png]]
  • [get | view] (2017-08-04 17:40:32, 7.2 KB) [[attachment:DirectedMovement.zip]]
  • [get | view] (2017-08-06 15:35:34, 6.4 KB) [[attachment:EpithelialSheet.zip]]
  • [get | view] (2017-08-04 20:15:18, 3.4 KB) [[attachment:HeikosModel (2).zip]]
  • [get | view] (2017-08-02 16:55:16, 1753.5 KB) [[attachment:Hirashima_et_al-2017-Development_Growt.pdf]]
  • [get | view] (2017-08-04 18:09:11, 132.3 KB) [[attachment:Josua-s Model Development Workflow Gene Expression Mammalian Blastocyst.pdf]]
  • [get | view] (2017-07-30 14:29:04, 29702.4 KB) [[attachment:Monday lectures Python ppt and code.zip]]
  • [get | view] (2017-08-02 19:58:27, 58286.7 KB) [[attachment:MultiScaleModelingWithCC3D-SBML.pptx]]
  • [get | view] (2017-08-01 18:49:13, 7433.6 KB) [[attachment:PBPK_SBML_modeling.pptx]]
  • [get | view] (2017-07-31 21:35:05, 38.2 KB) [[attachment:PK_simple_one_compartment.cps]]
  • [get | view] (2017-07-30 19:38:10, 738.3 KB) [[attachment:Priyom Verbal Model from Mark Alber Epithelial paper with annotations.docx]]
  • [get | view] (2017-07-27 15:34:10, 113.2 KB) [[attachment:PythonRefCard_1p.pdf]]
  • [get | view] (2017-07-27 15:34:51, 1634.3 KB) [[attachment:PythonRefCard_8p.pdf]]
  • [get | view] (2017-07-31 17:20:59, 19455.7 KB) [[attachment:Slides_ReactionKinetics-2.pptx]]
  • [get | view] (2017-07-30 17:31:42, 97.3 KB) [[attachment:WP_20170730_13_29_10_small.jpg]]
  • [get | view] (2017-07-31 18:01:31, 118.2 KB) [[attachment:WP_20170731_13_10_15_Pro.jpg]]
  • [get | view] (2017-07-27 16:27:11, 36.5 KB) [[attachment:beginners_python_cheat_sheet_pcc_SELECTED.png]]
  • [get | view] (2017-08-04 17:40:39, 13.1 KB) [[attachment:cellcycle_DN.zip]]
  • [get | view] (2017-08-04 17:47:09, 92.6 KB) [[attachment:class_photo.jpg]]
  • [get | view] (2017-08-02 19:59:01, 34.5 KB) [[attachment:delta-notch.cps]]
  • [get | view] (2017-08-02 20:05:06, 987.6 KB) [[attachment:delta-notch.pptx]]
  • [get | view] (2017-08-04 17:20:05, 5.8 KB) [[attachment:gradient_1.zip]]
  • [get | view] (2017-08-04 17:20:14, 6.9 KB) [[attachment:gradient_2.zip]]
  • [get | view] (2017-08-04 17:20:24, 6.6 KB) [[attachment:gradient_3.zip]]
  • [get | view] (2017-07-28 21:48:18, 3377.8 KB) [[attachment:holmes_et_al.gene_exp_enh_blas.pdf]]
  • [get | view] (2017-07-28 21:48:28, 2700.3 KB) [[attachment:holmes_sup_material.pdf]]
  • [get | view] (2017-07-31 13:25:51, 18008.4 KB) [[attachment:journal.pcbi.1005533.pdf]]
  • [get | view] (2017-07-30 14:57:48, 921.2 KB) [[attachment:lec1-intro.pptx]]
  • [get | view] (2017-07-30 14:57:40, 1.2 KB) [[attachment:lec1.py]]
  • [get | view] (2017-07-30 14:58:39, 76.8 KB) [[attachment:lec2-branching-iteration.pptx]]
  • [get | view] (2017-07-30 14:57:55, 2.8 KB) [[attachment:lec2.py]]
  • [get | view] (2017-07-30 14:58:57, 61.3 KB) [[attachment:lec3-strings-approximations.pptx]]
  • [get | view] (2017-07-30 14:58:46, 2.9 KB) [[attachment:lec3.py]]
  • [get | view] (2017-07-30 14:59:04, 1342.3 KB) [[attachment:lec4-basic-functions.pptx]]
  • [get | view] (2017-07-30 14:59:23, 2097.2 KB) [[attachment:lec5-tuple-lists.pdf]]
  • [get | view] (2017-07-30 14:59:15, 4.5 KB) [[attachment:lec5_tuples_lists.py]]
  • [get | view] (2017-07-30 14:59:47, 287.6 KB) [[attachment:lec6-dictionaries.pptx]]
  • [get | view] (2017-07-30 14:59:28, 4.6 KB) [[attachment:lec6.py]]
  • [get | view] (2017-07-30 14:59:39, 4.6 KB) [[attachment:lec6_recursion_dictionaries.py]]
  • [get | view] (2017-07-30 14:59:58, 20274.9 KB) [[attachment:lec7-random-walk.pptx]]
  • [get | view] (2017-08-09 16:50:37, 584.2 KB) [[attachment:parameter-scan.pdf]]
  • [get | view] (2017-07-27 16:26:57, 31.3 KB) [[attachment:python_refcard.png]]
  • [get | view] (2017-08-01 18:56:32, 3390.1 KB) [[attachment:s13628-015-0022-x.pdf]]
  • [get | view] (2017-07-28 18:54:30, 22.2 KB) [[attachment:schedule.docx]]
  • [get | view] (2017-07-28 18:54:20, 301.5 KB) [[attachment:schedule.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.