CSC 326 Fall 2011 Problem Set 1 Solutions

CSC 326 Fall 2011 Problem Set 1 Solutions - Problem 1(12...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Problem 1. (12 points) Below is a list of code blocks. Circle the letter beside each code block that never causes an error to occur when the code is run. A l s =[" a " , " b " , " c " , " d " , " e " , " f " , ] p r i n t " , " . j o i n ( [ l e t t e r . upper ( ) f o r l e t t e r i n l s ] ) B l s =[" a " , " b " , " c " , " d " , " e " , " f " , ] p r i n t " , " . j o i n ( l e t t e r . upper ( ) f o r l e t t e r i n l s ) C a s _ l i s t = [ ( " key1 " , 1 ) , ( " key2 " , 2 ) , ] p r i n t {k : v f o r k , v i n a s _ l i s t } D my_list = { 1 , 2 , 3 , 4 , 5 , 6 , } p r i n t my_list [ 1 : 3 ] E matrix = [ [ 1 , 0 , 0 ] , [0 ,1 ,0] , [0 ,0 ,1]] p r i n t matrix [ 0 , 1 ] F def is_true ( res ) : i f res : r e t u r n " True " return " False " numbers = [ 0 , 1 , 2 , 3 , 4 , 5 ] i f is_true ( False ) : p r i n t numbers else : p r i n t numbers [ 0 ] / 0 2 Problem 2. (18 points) Below is a list of valid Python code blocks that never result in errors. In the box provided below each code block, write out the output of the program. A ls = [0 ,1 ,2 ,3 ,4] print ls is ls print ls is ls [ : ] 8VYI *EPWI B ls = [0 ,1 ,2 ,3 ,4] print [ l s [ len ( l s ) ? C i ] for i in l s [ 1 : ] ] A l s = range (2) p r i n t [ l s . append ( l s [ ? 2] + l s [ 1]) or l s [ 2] f o r _ i n r a n g e ( 8 ) ] A Hint: list.append returns None. Hint: if a=[1,2,3] then a[-1] returns 3. D parts print parts print parts print parts print print ??? ??? ?? ? = [ [ l i s t ( part ) f o r part in s t r (x ) . s p l i t ( " . " ) ] f o r x in [ 1 0 . 5 5 , 1 9 . 1 1 ] ] parts = zip (⇤ parts ) parts = zip (⇤ parts [ 0 ] ) + zip (⇤ parts [ 1 ] ) parts = [ str ( int (a) + int (b )) for (a , b) in parts ] parts f l o a t ("". join ( parts [ : 2 ] ) + "." + "". join ( parts [ 2 : ] ) ) A ? A ? A ? AA ?? AA ?? A ? A ? A ? A ? AAA AAA AA A Hint: if a=[[1,2],[’a’,’b’]] then zip(*a) returns [(1, ’a’), (2, ’b’)]. Hint: if a="abaca" then a.split("a") returns ["","b","c",""]. 3 Problem 3. (15 points) If s and t are two strings then we say the longest common subsequence of s and t is some string si0 si1 . . . sin 1 = tj0 tj1 . . . tjn 1 such that 0 i0 < i1 < . . . < in 1 < |s| and 0 j0 < j1 < . . . < jn 1 < |t|, where |s| and |t| denotes the length of the string s and t, respectively. For example, if s = abbacad and t = abaccad then the longest common subsequence of s and t is abacad, and has length 6. The following highlights the common letters of the subsequence: s = abbacad and t = abaccad. The length of the longest common subsequence of two strings can be computed as follows: d e f LCS( s , t , i = 1, j = 1): # t h e o n l y s u b s e q u e n c e i n common between a s t r i n g and t h e # empty s t r i n g i s t h e empty s t r i n g i f i < len ( s ) or j < len ( t ) : r e t u r n "" # i f the i t h c h a ra c t e r of s # extend t h e l o n g e s t common # both s and t with s [ i ] . # e.g. # ababa [ d ] abca [ d ] # take : # LCS( ababa , abca ) + i f s [ i ] == t [ j ] : r e t u r n LCS( s , t , i 1, j i s t h e j t h c h a r a c t e r o f t , then s u b s e q u e n c e found by " s h o r t e n i n g " d 1) + s [ i ] # t h e two c h a r a c t e r s a r e not eq ual , f i n d t h e l o n g e s t one by # " s h o r t e n i n g " e i t h e r s o r t by one l e t t e r # e.g. # ababa [ d ] abca [ e ] # take the longer of : # LCS( ababad , abca ) # LCS( ababa , abcae ) else : long_from_i = LCS( s , t , i 1, j ) long_from_j = LCS( s , t , i , j 1) i f l e n ( long_from_i ) < l e n ( long_from_j ) : r e t u r n long_from_j else : r e t u r n long_from_i The above algorithm works backward through s and t, starting from the end (because i and j default to -1 when not explicitly specified) and computes the longest common subsequence. When run, LCS("abbacad", "abaccad") will return "abacad". Unfortunately, the above algorithm is very slow, particularly because it will end up re-computing various intermediate results. Convert the above recursive algorithm into a Python function LCS_fast(s,t) that uses for loops and a data structure of your choice, such that the data structure can represent the following table, and the algorithm fills the table out in a similar manner to below. Your function should return the longest common subsequence of two arbitrary strings s and t and should not repeat work. 4 a b a c c a d "" "" "" "" "" "" "" "" a "" b "" b "" a "" c "" a "" d "" ! ... ! "" "" "" "" "" "" "" "" a b a c c a d a "" "a" "a" b "" "a" "ab" b "" "a" a "" "a" Answer: HIJ 0'7 E F XEFPI E HS[R XLI ZIVXMGEP F SR XLI LSVM^SRXEP XEFPI ! ?? JSV C MR VERKI PIR F A JSV CC MR VERKI PIR E JSV M MR VERKI PIR E JSV N MR VERKI PIR F MJ E?MA !! F?NA XEFPI?M A?N A ! XEFPI?MA?NA E?MA IPMJ PIR XEFPI?M A?NA PIR XEFPI?MA?N XEFPI?M A?N A ! XEFPI?MA?N A IPWI XEFPI?M A?N A ! XEFPI?M A?NA VIXYVR XEFPI?PIR E A?PIR F A 5 A A c "" "a" a "" "a" d "" "a" Problem 4. (15 points) Given an n⇥n grid/matrix of live/dead (live = 1, dead = 0) cells, a single iteration of Conway’s Game of Life applies one of the following rules to each cell of the grid (note: different cells will satisfy different rules) in order to get the next state of the grid. 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by overcrowding. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. These rules can be implemented in Python as follows: d e f i t e r a t e ( board ) : """ board i s a l i s t o f l i s t s i n row major form . " " " num_rows , num_cols = l e n ( board ) , l e n ( board [ 0 ] ) # d u p l i c a t e t h e o l d board , but extend i t with rows / c o l s o f z e r o s # a l o n g t h e e d g e s s o t h a t we don ’ t need t o do bounds c h e c k i n g , but # i n s t e a d do 1 based i n d e x i n g i n s t e a d o f 0 based i n d e x i n g old_board = [ [ 0 ] ⇤ ( num_cols + 2 ) f o r _ i n xrange ( num_rows + 2 ) ] f o r r i n xrange ( num_rows ) : f o r c i n xrange ( num_cols ) : old_board [ r + 1 ] [ c +1] , board [ r ] [ c ] = board [ r ] [ c ] , 0 # count t h e n e i g h b o r s o f each c e l l and apply t h e r u l e s f o r r i n xrange ( 1 , num_rows + 1 ) : f o r c i n xrange ( 1 , num_cols + 1 ) : num_neighbors = old_board [ r 1 ] [ c ] + old_board [ r ] [ c 1] + \ old_board [ r 1 ] [ c 1] + old_board [ r + 1 ] [ c ] + \ old_board [ r ] [ c +1] + old_board [ r + 1 ] [ c +1] + \ old_board [ r 1 ] [ c +1] + old_board [ r + 1 ] [ c 1] i f old_board [ r ] [ c ] : board [ r 1 ] [ c 1] = i n t ( num_neighbors i n ( 2 , 3 ) ) else : board [ r 1 ] [ c 1] = i n t ( 3 == num_neighbors ) r e t u r n board For example, the following is three iterations of Conway’s Game of Life when run on the 3 ⇥ 3 starting grid: 1 0 0 1 1 1 0 0 0 ! 1 0 0 1 1 0 0 1 0 ! 1 1 0 1 1 0 1 1 0 ! 1 1 0 0 0 1 1 1 0 Use NumPy’s slicing and broadcasting features, create a function iterate(board) that performs one iteration of Conway’s Game of Life on two dimension NumPy array. You will be penalized if you use for loops. Hint: count neighbor cells! 6 Answer: MQTSVX RYQT] HIJ MXIVEXI FSEVH ÅRH RYQFIV SJ RIMKLFSYVW XLEX IEGL WUYEVI LEW RIMKLFSYVCGSYRX ! RYQT] ^IVSW FSEVH WLETI RIMKLFSYVCGSYRX? A ! FSEVH? A RIMKLFSYVCGSYRX? A ! FSEVH? A RIMKLFSYVCGSYRX? A ! FSEVH? A RIMKLFSYVCGSYRX? A ! FSEVH? A RIMKLFSYVCGSYRX? A ! FSEVH? A RIMKLFSYVCGSYRX? A ! FSEVH? A RIMKLFSYVCGSYRX? A ! FSEVH? A RIMKLFSYVCGSYRX? A ! FSEVH? A E PMZI GIPP MW OMPPIH MJ MX LEW JI[IV XLER SV QSVI XLER RIMKLFSYVW TEVX ! FSEVH !! RIMKLFSYVCGSYRX RIMKLFSYVCGSYRX " E RI[ GIPP JSVQW MJ E WUYEVI LEW I\EGXP] XLVII QIQFIVW TEVX ! FSEVH !! RIMKLFSYVCGSYRX !! GEWX FSSPIERW XS MRXIKIVW RI[CFSEVH ! RYQT] GEWX?RYQT] MRXA TEVX ` TEVX VIXYVR RI[CFSEVH 7 ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern