





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python review for DSA. Introducing algorithms
Typology: Study Guides, Projects, Research
1 / 9
This page cannot be seen from the preview
Don't miss anything!






a. lst = [ 1 , 2 , 3] lst2 = lst lst.append(4) lst2.append(5)
print(lst)
print(lst2)
b. s = "aBc" s = s.upper() t = s t = t.lower()
print(s)
print(t)
c. s = "abc"
def func(s): s = s.upper() print( "Inside func s =" , s)
func(s)
print(s)
d. lst = [ 1 , 2 , 3] def func(lst): lst.append(4) lst = [5, 6, 7, 8] print( "Inside func lst =" , lst)
func(lst)
print(lst)
class Student: def init( self , name = "student", age = 18): self .name = name self .age = age self .courses = []
def add_course( self , course): self .courses.append(course)
def remove_course( self , course): if course in self .courses:
peter.add_course( "Physics" ) peter.remove_course( "Spanish" )
tom = Student( "Tom Holland" ) tom.courses = peter.courses tom.add_course( "Economics" ) peter.remove_course( "Chemistry" ) print(peter.courses) print(tom.courses)
peter.name, tom.name = tom.name, peter.name print(peter.name, peter.age) print(tom.name, tom.age)
In this section, it is strongly recommended that you solve the problem on paper before writing code. This will be good practice for when you write code by hand on the exams.
def can_construct(word , letters): """ word - type: str letters - type: str return value - type: bool """
This function is passed in a string containing a word, and another string containing letters in your hand. When called, it will return True if the word can be constructed with the letters provided; otherwise, it will return False.
Notes: ● Each letter provided can only be used one. ● You may assume that the word and letters will only contain lower-case letters. ● You may not use a dictionary for this question. ● Hint : Try to think about how you can use a list to implement a dictionary.
ex) can_construct( "apples" , "aples" ) will return False.
ex) can_construct( "apples" , "aplespl" ) will return True.
If your Complex class works properly, you should see the following behavior:
def __ add __(self, other):
cplx1 + cplx
In this example, self refers to cplx1 since it is the first argument
and other would refer to cplx2 since it is the second argument.
'''
#constructor, output
cplx1 = Complex(5, 2)
print(cplx1) #5 + 2i
cplx2 = Complex(3, 3)
print(cplx2) #3 + 3i
#addition
print(cplx1 + cplx2) #8 + 5i
#subtraction
print(cplx1 - cplx2) #2 - 1i
#multiplication First Outer Inner Last
cplx1 * cplx (5 + 2i)(3 + 3i) -> multiply (53) + (53i) + (2i3) + (2i3i)
= 15 + 15i + 6i + 6(i^2) -> simplify = 15 + 21i + 6(-1) = 9 + 21i
print(cplx1 * cplx2) #9 + 21i #original objects remain unchanged
print(cplx1) #5 + 2i
print(cplx2) #3 + 3i
a. Implement a function: def create_permutation(n)
This function is given a positive integer n, and returns a list containing a random permutation of the numbers: 0, 1, 2, … , (n-1).
For example, one call to create_permutation(6) could return the list: [3, 2, 5, 4, 0, 1]. Another call to create_permutation(6) could return the list: [2, 0, 3, 1, 5, 4].
Implementation requirement: You may only use the randint function from the random module. Specifically, you are not allowed to use the shuffle function.
b. Implement a function: def scramble_word(word)
This function is given a string word, and returns a scrambled version of word, that is a new string containing a random reordering of the letters of word.
For example, one call to scramble_word( "pokemon" ) could return "okonmpe". Another call to scramble_word( "pokemon" ) could return "mpeoonk".
Implementation requirement: To determine the new order of the letters, call the function create_permutation.
For example, for the word "pokemon", the scrambled word implied by the permutation [1, 4, 5, 2, 3, 0, 6] is "omokepn" (since, the first letter is the letter from index 1, the second letter is the letter from index 4, the third letter is the letter from index 5, and so on).
c. Write a guessing game that takes a word, scrambles it, prints the letters to the user, and allows them three chances to guess the unscrambled word.
Have your program interact with the user as demonstrated below: Unscramble the word: o m o k e p n Try #1: openkom Wrong! Try #2: pokemon Yay, you got it!
Notes: You should use the functions you implemented in the previous sections. When printing the letters of the scrambled word, include a space between every two letters.