one slide summary l system
play

One-Slide Summary L-System Recursive transition networks and - PDF document

One-Slide Summary L-System Recursive transition networks and Backus-Naur Form context-free grammars are equivalent formalisms for Fractals specifying formal languages. find_closest is quite powerful. Problem sets? & L-system


  1. One-Slide Summary L-System • Recursive transition networks and Backus-Naur Form context-free grammars are equivalent formalisms for Fractals specifying formal languages. • find_closest is quite powerful. Problem sets? & • L-system fractals are based on a rewriting system that Procedure is very similar to BNF grammars. Practice • We can practice our CS knowledge up to this point to solve problems by writing recursive procedures . (It won't take too long.) #1 #2 Outline Recursive Transition Networks • Briefly: Recursive Transition Networks ORNATE NOUN – vs. Backus-Naur Form Grammars • Problem Sets – Revenge of find_closest NOUN end begin ARTICLE ADJECTIVE • PS3 L-System Fractals ORNATE NOUN ::= OPTARTICLE ADJECTIVES NOUN • Solving Problems ADJECTIVES ::= ADJECTIVE ADJECTIVES – Problem Representation ADJECTIVES ::= ε – Important Functions OPTARTICLE ::= ARTICLE Recall: the two notations OPTARTICLE ::= ε are equivalent. #3 #4 find_closest live demo Problem Sets • Not just meant to review stuff you should • Let's code it together now in PyCharm. already know – Get you to explore new ideas find_closest (goal, lst, dist) – Motivate what is coming up in the class • The main point of the PSs is learning , not • Hint: let's make test cases first. evaluation – Don’t give up if you can’t find the answer in the book (you won’t solve many problems this way) – Do discuss with other students – (This is why they are difficult.) #5 #6

  2. PS2: Question 1 PS2: Question 4: Creativity 1.i. len([1,2,3][0]) def seq_comp(lst): = len(1) if not lst: return [] = error: 1 is not a list! return [nuc_comp(lst[0])] + seq_comp(lst[1:]) 1.q. map(len,[ [1,2,3], [4,5], [6] ]) def seq_comp(lst): = [len([1,2,3]), len([4,5]), len([6])] return map(nuc_comp, lst) = [3, 2, 1] def seq_comp(lst): return [nuc_comp(nuc) for nuc in lst] #7 #8 Liberal Arts Trivia: Liberal Arts Trivia: Media Studies Latin American Studies • This important leader of Spanish America's • This 1988 book by Herman and Chomsky presented the seminal “propaganda model”, arguing that as news media outlets are run successful struggle for independence is by corporations, they are under competitive pressure. Consider the credited with decisively contributing to the dependency of mass media news outlets upon major sources of news, particularly the government. If a particular outlet is in independence of the present-day countries of disfavor with a government, it can be subtly 'shut out', and other Venezuela, Colombia, Ecuador, Peru, outlets given preferential treatment. Since this results in a loss in news leadership, it can also result in a loss of viewership. That can Panama, and Bolivia. He defeated the Spanish itself result in a loss of advertising revenue, which is the primary Monarchy and was in turn defeated by income for most of the mass media (newspapers, magazines, television). To minimize the possibilities of lost revenue, tuberculosis. therefore, outlets will tend to report news in a tone more favorable to government and business, and giving unfavorable news about government and business less emphasis. #9 #10 L-Systems CommandSequence ::= [ CommandList ] CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= R Angle Command ::= O CommandSequence #12

  3. CommandSequence ::= [ CommandList ] CommandList ::= Command CommandList L-System CommandList ::= Command ::= F Rewriting Command ::= R Angle Command ::= O CommandSequence Start: [F] Rewrite Rule: F  [F O[R30 F] F O[R-60 F] F] Work like BNF replacement rules, Level 1 Level 0 except replace all instances at once! Start: [F] F  [F O[R30 F] F O[R-60 F] F] Why is this a better model for biological systems? [F] [F O[R30 F] F O[R-60 F] F] #13 The Great Lambda Tree of Ultimate Knowledge and Infinite Power Level 2 Level 3 (Level 5 with color) #15 #16 Previous CS 1120 Students: Tree Outside My Window A Heart #18

  4. PS3 - Fractals Procedure Practice • In addition to completing the problem set, • For the rest of this class, we will be each team will submit its prettiest fractal. practicing writing recursive procedures • The class will then vote for favorites, and together. the authors of the favorites will receive • Write a procedure count-fives that takes as extra credit. input a list of numbers. It returns the number • No Photoshop, etc. All PS3. of fives contained in its input list. – You just change the rules: – count_fives([1, 2, 3, 4, 5,]) -> 1 •F  [F O[R30 F] F O[R-60 F] F] # one fractal – count_fives([5, -5, 5, 7]) -> 2 •F  [O[R60 F] F F O[R45 F]] # a new one! – count_fives([ ] ) -> 0 – count_fives([8, 6, 7, 5, 3, 0, 9]) -> 1 #19 #20 Hints Three versions of count_fives • Remember our strategy! def count_fives (lst): • Be optimistic! if not lst: return 0 if lst[0] == 5: return 1 + count_fives(lst[1:]) – Assume that you can write “count_fives” – So the recursive case will work out return count_fives(lst[1:]) • Identify the smallest input you can solve All work fine! How are they def count_fives (lst): – The base case different? if not lst: return 0 • How would you combine answers return (1 if lst[0] == 5 else 0) + count_fives(lst[1:]) – From the current call (usually lst[0] ) – And the result of the recursive call (on lst[1:] ) def count_fives (lst): • Be creative! There are usually many solutions. return len(filter(lambda x : x == 5, lst)) #21 #22 Liberal Arts Trivia: Cognitive Liberal Arts Trivia: Medicine Psychology • This vector-borne infectious disease is caused • This American psychologist coined the term by protozoan parasites. It is widespread in Cognitive Psychology in his late 1960's book of tropical regions, such as sub-Saharan African. the same name. He was critical of linear Each year there are about 515 million cases of programming models of psychology, felt that it, killing between one and three million psychology should address everyday concerns, people. No formal vaccine is available. Classic and respected the direct perception theories symptoms include sudden coldness followed of J.J. And Eleanor Gibson. He headed the by rigor and then fever and sweating. APA task force that reviewed The Bell Curve . #23 #24

  5. Liberal Arts Trivia: Accounting contains • In this bookkeeping system, each transaction • Write a procedure contains that takes two is recorded in at least two accounts. Each arguments: an element and a list. It returns transaction results in one account being True if the list contains the given element, debited and another account being credited, False otherwise. with the total debits equal to the total – contains(5, [1, 2, 3, 4]) -> False credits. Luca Pacioli, a monk and collaborator – contains(5, [2, 3, 4, 5]) -> True of Leonardo da Vinci, is called the “father of – contains([], [1, 2, 3]) -> False accounting” because he published a usable, – contains([], [1, 2, []]) -> True detailed description of this system. – contains(1, [2, [], 1]) -> True – contains(3, [ ]) -> False #25 #26 contains explained common_elt • Write a procedure common_elt that takes def contains (elt, lst): two lists as arguments. It returns True if there if not lst: return False is a common element contained in both lists, elif lst[0] == elt: return True All work fine! False otherwise. How are they else: return contains(elt, lst[1:]) different? – common_elt([1, 2, 3], [3, 4, 5]) -> True – common_elt([1, 2, 3], [4, 5, 6]) -> False def contains (elt, lst): – common_elt([1, 2], [0, 0, 0, 1]) -> True if not lst: return False – common_elt([1], []) -> False return (lst[0] == elt) or contains(elt, lst[1:]) – common_elt([], [1,2,3]) -> False – common_elt([], []) -> False def contains (elt, lst): • Hint: You can use contains. return filter(lambda x : x == elt, lst) != [] #27 #28 common_elt? zero2hero def common_elt (lst1, lst2) • Write a procedure zero2hero that takes as if not lst1: return False All work! input a list of strings. It returns the same list How are they elif contains(lst1[0], lst2): return True different? in the same order, but every element that else: return common_elt(lst1[1:], lst2) used to be “zero” is now “hero”. def common_elt (lst1, lst2) – zero2hero([“a”, “zero”, “b”, “jercules”]) if (not lst1) or (not lst2): return False -> [“a”, “hero”, “b”, “jercules”] return (lst1[0] == lst2[0]) or common_elt(lst1,lst2[1:]) or \ – zero2hero([“zorro”]) common_elt(lst1[1:], lst2) # this version is super slow! -> [“zorro”] – zero2hero([“zero”, “zero”, “one”, “zero”]) def common_elt (lst1, lst2): -> [“hero”, “hero”, “one”, “hero”] return filter(lambda e1 : contains(e1, lst2), lst1) != [] #29 #30

Recommend


More recommend