labNo
float64
1
10
taskNo
float64
0
4
questioner
stringclasses
2 values
question
stringlengths
9
201
code
stringlengths
18
30.3k
startLine
float64
0
192
endLine
float64
0
196
questionType
stringclasses
4 values
answer
stringlengths
2
905
src
stringclasses
3 values
code_processed
stringlengths
12
28.3k
id
stringlengths
2
5
raw_code
stringlengths
20
30.3k
raw_comment
stringlengths
10
242
comment
stringlengths
9
207
q_code
stringlengths
66
30.3k
3
3
TA
In task4, can you explain how do you check blanks are window or corner?
from cs1robots import * def turn_left(): global dx, dy dx, dy = -dy, dx my_robot.turn_left() def turn_right(): for i in range(3): turn_left() def move_further() : global x, y x += dx y += dy my_robot.move() def go_back() : for i in range(2) : turn_left() move_further() my_robot.drop_beeper() turn_right() move_further() load_world('worlds/rain2.wld') # load_world('worlds/rain2.wld') my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_pause(0.1) my_robot.set_trace("blue") x, y, dx, dy = 2, 6, 1, 0 counter = 0 move_further() initial_pos = (x,y) turn_right() move_further() while not (x, y) == initial_pos : if my_robot.right_is_clear() and my_robot.left_is_clear() : counter += 1 else : counter = 0 if counter == 2 : counter = 0 go_back() if my_robot.right_is_clear() : turn_right() move_further() elif my_robot.front_is_clear() : move_further() else : turn_left() move_further()
38
53
code_explain
Okay, so I mainly try to go right if it is possible. And then there are two cases: 1) window 2) corner if it is a window then when I turn right and go forward, left and right of the robot must still be empty. If it is a corner, the right of the robot must not be empty. The variable counter keeps this information for me. When I decide that the robot is outside (went out from a window) I backtrack it and place a beeper on the window.
cs1qa
null
null
null
null
null
Question: In task4, can you explain how do you check blanks are window or corner? Code: from cs1robots import * def turn_left(): global dx, dy dx, dy = -dy, dx my_robot.turn_left() def turn_right(): for i in range(3): turn_left() def move_further() : global x, y x += dx y += dy my_robot.move() def go_back() : for i in range(2) : turn_left() move_further() my_robot.drop_beeper() turn_right() move_further() load_world('worlds/rain2.wld') # load_world('worlds/rain2.wld') my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_pause(0.1) my_robot.set_trace("blue") x, y, dx, dy = 2, 6, 1, 0 counter = 0 move_further() initial_pos = (x,y) turn_right() move_further() while not (x, y) == initial_pos : if my_robot.right_is_clear() and my_robot.left_is_clear() : counter += 1 else : counter = 0 if counter == 2 : counter = 0 go_back() if my_robot.right_is_clear() : turn_right() move_further() elif my_robot.front_is_clear() : move_further() else : turn_left() move_further()
4
1
TA
When does the is_triangle() function return True in Task2?
def is_triangle(a, b, c): if a+b>c and b+c>a and c+a>b: return True return False a = float(input('Side a: ')) b = float(input('Side b: ')) c = float(input('Side c: ')) # all because this is dynamic language. # type conversion by float() was inevitable... totally bonkers. if is_triangle(a, b, c): print("YES") else: print("NO")
0
3
variable
Returns True if the trigonometric inequalities are in turn for all sides.
cs1qa
null
null
null
null
null
Question: When does the is_triangle() function return True in Task2? Code: def is_triangle(a, b, c): if a+b>c and b+c>a and c+a>b: return True return False a = float(input('Side a: ')) b = float(input('Side b: ')) c = float(input('Side c: ')) # all because this is dynamic language. # type conversion by float() was inevitable... totally bonkers. if is_triangle(a, b, c): print("YES") else: print("NO")
4
2
TA
Task 3: Is the parameter of the sine function degree or radian?
import math sin = math.sin pi = math.pi a= int(input('How many steps? ')) for i in range(a): x = float(i) / float(a-1) * 2 * pi print (sin(x))
6
7
variable
Task 3: The argument of the sine function is converted to radians by multiplying by 2*pi.
cs1qa
null
null
null
null
null
Question: Task 3: Is the parameter of the sine function degree or radian? Code: import math sin = math.sin pi = math.pi a= int(input('How many steps? ')) for i in range(a): x = float(i) / float(a-1) * 2 * pi print (sin(x))
5
0
TA
Why should I use global balance in task1?
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! global balance balance = balance + money #pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! global balance if balance < money: print("You've withdrawn", money, "won") print("But you only have", balance, "won") else: balance = balance - money print("You withdraw", money, "won") #pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: d = 'd' w = 'w' c = 'c' process = input("Deposit(d) or withdrawal(w) or balance check(c)? If you want to finish, just press enter") if process == d: money = int(input("How much do you want to deposit?")) deposit(money) print("You deposited", money, "won") elif process == w: money = int(input("How much do you want to withdraw?")) withdrawal(money) elif process == c: print("Your current balance is", balance, "won") elif process == '': break else: print("Please, press d or w or c or return") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
12
12
reasoning
I used it because I had to change the value of the balance (because I have to use it, not just read it)!
cs1qa
null
null
null
null
null
Question: Why should I use global balance in task1? Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! global balance balance = balance + money #pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! global balance if balance < money: print("You've withdrawn", money, "won") print("But you only have", balance, "won") else: balance = balance - money print("You withdraw", money, "won") #pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: d = 'd' w = 'w' c = 'c' process = input("Deposit(d) or withdrawal(w) or balance check(c)? If you want to finish, just press enter") if process == d: money = int(input("How much do you want to deposit?")) deposit(money) print("You deposited", money, "won") elif process == w: money = int(input("How much do you want to withdraw?")) withdrawal(money) elif process == c: print("Your current balance is", balance, "won") elif process == '': break else: print("Please, press d or w or c or return") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
3
0
TA
What is the orient function in task1?
from cs1robots import * create_world(avenues = 4, streets = 7) hubo=Robot(orientation = 'W', avenue = 3, street = 5) hubo.set_trace('blue') def orient(): while hubo.facing_north() == False: hubo.turn_left() hubo.turn_left() def move(): while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() orient() move()
5
8
variable
I have set up the code to make the direction look east.
cs1qa
null
null
null
null
null
Question: What is the orient function in task1? Code: from cs1robots import * create_world(avenues = 4, streets = 7) hubo=Robot(orientation = 'W', avenue = 3, street = 5) hubo.set_trace('blue') def orient(): while hubo.facing_north() == False: hubo.turn_left() hubo.turn_left() def move(): while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() orient() move()
1
1
TA
OK~ Please explain why the input value is float and int like float(a) in task2 and int(k) in task3~
def is_triangle(a, b, c): if float(a)+float(b)>float(c) and float(a)+float(c)>float(b) and float(c)+float(b)>float(a): return True else: return False a = input('Side a: ') b = input('Side b: ') c = input('Side c: ') if is_triangle(a, b, c): print('YES') else: print('Again')
1
1
reasoning
Otherwise, it is recognized as a string. Then the meaning as a number disappears.
cs1qa
null
null
null
null
null
Question: OK~ Please explain why the input value is float and int like float(a) in task2 and int(k) in task3~ Code: def is_triangle(a, b, c): if float(a)+float(b)>float(c) and float(a)+float(c)>float(b) and float(c)+float(b)>float(a): return True else: return False a = input('Side a: ') b = input('Side b: ') c = input('Side c: ') if is_triangle(a, b, c): print('YES') else: print('Again')
2
2
TA
What does jump_one_hurdle in Task 3 do?
from cs1robots import * load_world('worlds/hurdles1.wld') #load_world('worlds/hurdles2.wld') #load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace("blue") a=0 def turnright(): my_robot.turn_left() my_robot.turn_left() my_robot.turn_left() def check(): if my_robot.on_beeper()==True: a=1 else: a=0 def jump_one_hurdle(): if my_robot.front_is_clear()==False: my_robot.turn_left() my_robot.move() turnright() my_robot.move() turnright() my_robot.move() my_robot.turn_left() else: my_robot.move() while my_robot.on_beeper()==False: jump_one_hurdle()
18
29
variable
If there is a wall in front of the robot, it goes over the wall, and if there is no wall, it moves forward one space.
cs1qa
null
null
null
null
null
Question: What does jump_one_hurdle in Task 3 do? Code: from cs1robots import * load_world('worlds/hurdles1.wld') #load_world('worlds/hurdles2.wld') #load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace("blue") a=0 def turnright(): my_robot.turn_left() my_robot.turn_left() my_robot.turn_left() def check(): if my_robot.on_beeper()==True: a=1 else: a=0 def jump_one_hurdle(): if my_robot.front_is_clear()==False: my_robot.turn_left() my_robot.move() turnright() my_robot.move() turnright() my_robot.move() my_robot.turn_left() else: my_robot.move() while my_robot.on_beeper()==False: jump_one_hurdle()
2
1
TA
I'm going to check it now. Can you explain how you made it for each task in the meantime?
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=6) hubo.set_trace("blue") def plant(): if not hubo.on_beeper(): hubo.drop_beeper() def st1(): for i in range(5): hubo.move() plant() hubo.turn_left() hubo.move() plant() hubo.turn_left() for i in range(5): hubo.move() plant() for i in range(3): hubo.turn_left() hubo.move() plant() for i in range(3): hubo.turn_left() hubo.move() for i in range(2): st1() for i in range(5): hubo.move() plant() hubo.turn_left() hubo.move() plant() hubo.turn_left() for i in range(5): hubo.move() plant()
9
39
code_explain
In task2, in the function of task1, if there is no beeper, only the function is changed to leave the beeper.
cs1qa
null
null
null
null
null
Question: I'm going to check it now. Can you explain how you made it for each task in the meantime? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=6) hubo.set_trace("blue") def plant(): if not hubo.on_beeper(): hubo.drop_beeper() def st1(): for i in range(5): hubo.move() plant() hubo.turn_left() hubo.move() plant() hubo.turn_left() for i in range(5): hubo.move() plant() for i in range(3): hubo.turn_left() hubo.move() plant() for i in range(3): hubo.turn_left() hubo.move() for i in range(2): st1() for i in range(5): hubo.move() plant() hubo.turn_left() hubo.move() plant() hubo.turn_left() for i in range(5): hubo.move() plant()
10
0
TA
Please explain briefly one by one what the attributes of the created Car class are.
from cs1graphics import * from time import sleep canvas = Canvas (900, 600) canvas.setBackgroundColor("light blue") class Car(object): def __init__(self, bodycolor='Blue', wheelcolor='Grey',state = 'slow' ): self.bodycolor = bodycolor self.wheelcolor = wheelcolor self.state = state car = Layer() wheel1 = Layer() wheel2 = Layer() tire1 = Circle(20, Point(-40,-10)) tire1.setFillColor('black') wheel1a = Rectangle(30,5, Point(0,0)) wheel1a.setFillColor(wheelcolor) wheel1b = Rectangle(5,30, Point(0,0)) wheel1b.setFillColor(wheelcolor) car.add(tire1) wheel1.add(wheel1a) wheel1.add(wheel1b) tire2 = Circle(20, Point(40,-10)) tire2.setFillColor('black') wheel2a = Rectangle(30,5, Point(0,0)) wheel2a.setFillColor(wheelcolor) wheel2b = Rectangle(5,30, Point(0,0)) wheel2b.setFillColor(wheelcolor) car.add(tire2) wheel2.add(wheel2a) wheel2.add(wheel2b) body = Rectangle(140, 60, Point(0,-50)) body.setFillColor(bodycolor) body.setDepth(60) car.add(body) canvas.add(car) car.moveTo(200,200) canvas.add(wheel1) wheel1.moveTo(160,190) canvas.add(wheel2) wheel2.moveTo(240,190) self.car = car self.body = body self.wheel1 = wheel1 self.wheel2 = wheel2 def move(self, x, y): if self.state == 'fast': smoke1 = Rectangle(30, 10, Point(-85,-30)) smoke1.setFillColor('grey') smoke1.setDepth(50) self.car.add(smoke1) smoke2 = Rectangle(20, 10, Point(-120,-30)) smoke2.setFillColor('grey') smoke2.setDepth(50) self.car.add(smoke2) self.body.setFillColor('dark blue') self.car.move(x, y) self.wheel1.move(x, y) self.wheel2.move(x, y) def rotate(self,x): self.wheel1.rotate(x) self.wheel2.rotate(x) # def draw_animal(): # # Implement this function. # global car # global wheel1 # global wheel2 # tire1 = Circle(20, Point(-40,-10)) # tire1.setFillColor('black') # wheel1a = Rectangle(30,5, Point(0,0)) # wheel1a.setFillColor("grey") # wheel1b = Rectangle(5,30, Point(0,0)) # wheel1b.setFillColor("grey") # car.add(tire1) # wheel1.add(wheel1a) # wheel1.add(wheel1b) # tire2 = Circle(20, Point(40,-10)) # tire2.setFillColor('black') # wheel2a = Rectangle(30,5, Point(0,0)) # wheel2a.setFillColor("grey") # wheel2b = Rectangle(5,30, Point(0,0)) # wheel2b.setFillColor("grey") # car.add(tire2) # wheel2.add(wheel2a) # wheel2.add(wheel2b) # body = Rectangle(140, 60, Point(0,-50)) # body.setFillColor("blue") # body.setDepth(60) # car.add(body) # canvas.add(car) # car.moveTo(200,200) # canvas.add(wheel1) # wheel1.moveTo(160,190) # canvas.add(wheel2) # wheel2.moveTo(240,190) # pass def show_animation(): # Implement this function. global car global wheel for i in range(100): car.move(2,0) car.rotate(5) sleep(0.01) sleep(0.5) car.state="fast" print(car.state) for i in range(200): car.move(7,0) car.rotate(10) sleep(0.01) pass car = Car() show_animation()
7
51
variable
The remaining attributes are car, body, wheel1, and wheel2, which represent the whole car, the body of the car, and two squashes respectively.
cs1qa
null
null
null
null
null
Question: Please explain briefly one by one what the attributes of the created Car class are. Code: from cs1graphics import * from time import sleep canvas = Canvas (900, 600) canvas.setBackgroundColor("light blue") class Car(object): def __init__(self, bodycolor='Blue', wheelcolor='Grey',state = 'slow' ): self.bodycolor = bodycolor self.wheelcolor = wheelcolor self.state = state car = Layer() wheel1 = Layer() wheel2 = Layer() tire1 = Circle(20, Point(-40,-10)) tire1.setFillColor('black') wheel1a = Rectangle(30,5, Point(0,0)) wheel1a.setFillColor(wheelcolor) wheel1b = Rectangle(5,30, Point(0,0)) wheel1b.setFillColor(wheelcolor) car.add(tire1) wheel1.add(wheel1a) wheel1.add(wheel1b) tire2 = Circle(20, Point(40,-10)) tire2.setFillColor('black') wheel2a = Rectangle(30,5, Point(0,0)) wheel2a.setFillColor(wheelcolor) wheel2b = Rectangle(5,30, Point(0,0)) wheel2b.setFillColor(wheelcolor) car.add(tire2) wheel2.add(wheel2a) wheel2.add(wheel2b) body = Rectangle(140, 60, Point(0,-50)) body.setFillColor(bodycolor) body.setDepth(60) car.add(body) canvas.add(car) car.moveTo(200,200) canvas.add(wheel1) wheel1.moveTo(160,190) canvas.add(wheel2) wheel2.moveTo(240,190) self.car = car self.body = body self.wheel1 = wheel1 self.wheel2 = wheel2 def move(self, x, y): if self.state == 'fast': smoke1 = Rectangle(30, 10, Point(-85,-30)) smoke1.setFillColor('grey') smoke1.setDepth(50) self.car.add(smoke1) smoke2 = Rectangle(20, 10, Point(-120,-30)) smoke2.setFillColor('grey') smoke2.setDepth(50) self.car.add(smoke2) self.body.setFillColor('dark blue') self.car.move(x, y) self.wheel1.move(x, y) self.wheel2.move(x, y) def rotate(self,x): self.wheel1.rotate(x) self.wheel2.rotate(x) # def draw_animal(): # # Implement this function. # global car # global wheel1 # global wheel2 # tire1 = Circle(20, Point(-40,-10)) # tire1.setFillColor('black') # wheel1a = Rectangle(30,5, Point(0,0)) # wheel1a.setFillColor("grey") # wheel1b = Rectangle(5,30, Point(0,0)) # wheel1b.setFillColor("grey") # car.add(tire1) # wheel1.add(wheel1a) # wheel1.add(wheel1b) # tire2 = Circle(20, Point(40,-10)) # tire2.setFillColor('black') # wheel2a = Rectangle(30,5, Point(0,0)) # wheel2a.setFillColor("grey") # wheel2b = Rectangle(5,30, Point(0,0)) # wheel2b.setFillColor("grey") # car.add(tire2) # wheel2.add(wheel2a) # wheel2.add(wheel2b) # body = Rectangle(140, 60, Point(0,-50)) # body.setFillColor("blue") # body.setDepth(60) # car.add(body) # canvas.add(car) # car.moveTo(200,200) # canvas.add(wheel1) # wheel1.moveTo(160,190) # canvas.add(wheel2) # wheel2.moveTo(240,190) # pass def show_animation(): # Implement this function. global car global wheel for i in range(100): car.move(2,0) car.rotate(5) sleep(0.01) sleep(0.5) car.state="fast" print(car.state) for i in range(200): car.move(7,0) car.rotate(10) sleep(0.01) pass car = Car() show_animation()
3
1
TA
Which part of task2 puts down all beepers?
from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash1.wld" ) # load_world( "worlds/trash2.wld" ) hubo = Robot() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def turn(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn() while hubo.front_is_clear(): hubo.move() turn_right() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn() hubo.move()
24
25
code_explain
This is a while statement that puts down the beeper until the carries_beepers() function returns false.
cs1qa
null
null
null
null
null
Question: Which part of task2 puts down all beepers? Code: from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash1.wld" ) # load_world( "worlds/trash2.wld" ) hubo = Robot() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def turn(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn() while hubo.front_is_clear(): hubo.move() turn_right() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn() hubo.move()
6
2
TA
Why is the condition like that in print_cards?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if(0 <= num1 <= 23 and 0 <= num2 <= 23 and num1 != num2 and not num1 in correct_list and not num2 in correct_list): return True ########################################################################### return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. correct_list.append(num1) correct_list.append(num2) print_cards() time.sleep(1) if(cards[num1][1] == cards[num2][1]): return True else: correct_list.remove(num1) correct_list.remove(num2) ########################################################################### print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while (len(correct_list) != 24): # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if(tries%10==1): print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==2): print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==3): print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") tries = tries+1 ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
45
45
reasoning
This is because cards with numbers in the correct list must be turned over, cards with numbers that are not in the correct list must be turned over.
cs1qa
null
null
null
null
null
Question: Why is the condition like that in print_cards? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if(0 <= num1 <= 23 and 0 <= num2 <= 23 and num1 != num2 and not num1 in correct_list and not num2 in correct_list): return True ########################################################################### return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. correct_list.append(num1) correct_list.append(num2) print_cards() time.sleep(1) if(cards[num1][1] == cards[num2][1]): return True else: correct_list.remove(num1) correct_list.remove(num2) ########################################################################### print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while (len(correct_list) != 24): # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if(tries%10==1): print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==2): print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==3): print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") tries = tries+1 ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
8
0
TA
How did you write the merge function in Task 1?
import elice_utils from time import sleep def merge(input_filenames, output_filename): with open(output_filename, 'w') as f: for filename in input_filenames: file = open(filename, 'r') for line in file.readlines(): f.write(line) file.close() merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
3
9
variable
Simply open each input file and write it to a file (f) that combines the contents.
cs1qa
null
null
null
null
null
Question: How did you write the merge function in Task 1? Code: import elice_utils from time import sleep def merge(input_filenames, output_filename): with open(output_filename, 'w') as f: for filename in input_filenames: file = open(filename, 'r') for line in file.readlines(): f.write(line) file.close() merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
6
1
TA
Is there any reason you used for _ in line 14?
import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ lst = [] for _ in range(trials): lst.append(random.randint(lb,ub)) return lst def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ return sum(num_list) / len(num_list) def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ lst = [] s = min(num_list) e = max(num_list) for i in range(s,e+1): lst.append((i,num_list.count(i))) return lst # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
13
15
reasoning
The variable used in the for-loop is not used anywhere, so I wrote it with a name that is not used well/
cs1qa
null
null
null
null
null
Question: Is there any reason you used for _ in line 14? Code: import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ lst = [] for _ in range(trials): lst.append(random.randint(lb,ub)) return lst def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ return sum(num_list) / len(num_list) def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ lst = [] s = min(num_list) e = max(num_list) for i in range(s,e+1): lst.append((i,num_list.count(i))) return lst # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
6
2
TA
How did you implement the two cards to show a picture when checking two cards?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if correct_list.count(i) != 0: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): n = 0 if 0<= num1 <len(num_pads) and 0<= num2 <len(num_pads): n +=1 if num1 != num2: n +=1 if correct_list.count(num1) == 0 and correct_list.count(num2) == 0: n +=1 ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if n == 3: return True else: return False def check(num1, num2): a=len(correct_list) canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if correct_list.count(i) != 0 or i == num1 or i == num2: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if cards[num1][1] == cards[num2][1]: correct_list.append(num1) correct_list.append(num2) ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### print_cards() if a+2 == len(correct_list): return True else: return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) print_cards() ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") if len(correct_list)==24: break else: tries =tries+1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
104
116
code_explain
I modified the code of the print_cards function, so that the photos in the correct_list and or when num1 and num2 are displayed in the condition that the photos are visible!
cs1qa
null
null
null
null
null
Question: How did you implement the two cards to show a picture when checking two cards? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if correct_list.count(i) != 0: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): n = 0 if 0<= num1 <len(num_pads) and 0<= num2 <len(num_pads): n +=1 if num1 != num2: n +=1 if correct_list.count(num1) == 0 and correct_list.count(num2) == 0: n +=1 ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if n == 3: return True else: return False def check(num1, num2): a=len(correct_list) canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if correct_list.count(i) != 0 or i == num1 or i == num2: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if cards[num1][1] == cards[num2][1]: correct_list.append(num1) correct_list.append(num2) ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### print_cards() if a+2 == len(correct_list): return True else: return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) print_cards() ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") if len(correct_list)==24: break else: tries =tries+1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
1
3
TA
Please explain task 4!
from cs1robots import* load_world('worlds/harvest1.wld') hubo=Robot(beepers=36) hubo.move() hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() for i in range(3): for j in range(5): hubo.pick_beeper() hubo.move() hubo.pick_beeper() turn_right() hubo.move() turn_right() for j in range(5): hubo.pick_beeper() hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left()
0
26
code_explain
Because task4 needs to pass all the yellow beeper and minimize the path I thought about how to go by moving in zigzag At this time, the process of moving up/down and the process of changing direction I used the loop in.
cs1qa
null
null
null
null
null
Question: Please explain task 4! Code: from cs1robots import* load_world('worlds/harvest1.wld') hubo=Robot(beepers=36) hubo.move() hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() for i in range(3): for j in range(5): hubo.pick_beeper() hubo.move() hubo.pick_beeper() turn_right() hubo.move() turn_right() for j in range(5): hubo.pick_beeper() hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left()
2
2
TA
Why do you need the if-else statement in lines 28-31 of Task 3?
from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. load_world('worlds/hurdles1.wld') # load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace('blue') def jump_one_hurdle(): my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() def turn_right(): for i in range(3): my_robot.turn_left() while not my_robot.on_beeper(): if my_robot.front_is_clear(): my_robot.move() else: jump_one_hurdle()
27
30
reasoning
If the front of the robot is empty, proceed until the hurdles come out, and if the front is blocked, you have to cross the hurdles.
cs1qa
null
null
null
null
null
Question: Why do you need the if-else statement in lines 28-31 of Task 3? Code: from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. load_world('worlds/hurdles1.wld') # load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace('blue') def jump_one_hurdle(): my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() def turn_right(): for i in range(3): my_robot.turn_left() while not my_robot.on_beeper(): if my_robot.front_is_clear(): my_robot.move() else: jump_one_hurdle()
10
0
TA
What is a bird??(What variable does it contain?)
from cs1graphics import * from time import sleep _scene = None _world = None t = 0.2 def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor('green') grass.setDepth(100) _scene.add(grass) #blocks block = Rectangle(40, 40, Point(200, 100)) block.setFillColor('brown') qmark = Text("?", 20, Point(200, 100)) qmark.setFontColor('Yellow') qmark.setDepth(48) _scene.add(qmark) block2 = block.clone() block2.move(40, 0) block.setDepth(50) _scene.add(block) _scene.add(block2) #pipe pipe = Polygon(Point(400, 150), Point(400, 160), Point(410, 160), Point(410, 320), Point(470, 320), Point(470, 160), Point(480, 160), Point(480, 150)) pipe.setFillColor('lightgreen') pipe.setDepth(10) pipe.move(-10, 0) _scene.add(pipe) class Mushroom(object): def __init__(self, x=200, y=92): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(lowermush) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom _scene.add(self.layer) def diappear(self): self.layer.scale(0.001) def move(self, x, y): self.layer.move(x, y) def arise(self): self.layer.setDepth(45) self.layer.move(0, -20) COLOR = ['Red', 'Blue'] TYPE = ['super', 'normal'] class Mario(object): def __init__(self, color='Blue', type='normal'): assert type in TYPE and color in COLOR self.color = color self.type = type self.step_size = 3 # Constructing Mario mario = Layer() # body body = Rectangle(33, 22, Point(200, 200)) body.setFillColor(color) body.setDepth(50) mario.add(body) # face face = Ellipse(30, 20, Point(200, 180)) face.setFillColor('beige') face.setDepth(40) mario.add(face) #hat hat = Polygon(Point(185, 175), Point(220, 175), Point(220, 173), Point(215, 173), Point(212, 168), Point(188, 168)) hat.setFillColor(color) hat.setDepth(39) mario.add(hat) #beard beard = Polygon(Point(207, 183), Point(217, 183), Point(215, 180), Point(209, 180)) beard.setFillColor('Brown') beard.setDepth(38) mario.add(beard) shoe = Layer() #left shoe lshoe = Rectangle(15, 6, Point(191, 215)) lshoe.setFillColor('black') lshoe.setDepth(52) shoe.add(lshoe) #right shoe rshoe = lshoe.clone() rshoe.move(17, 0) shoe.add(rshoe) mario.add(shoe) # save alias of moveable parts self.layer = mario self.body = body self.hat = hat self.shoe = shoe _scene.add(self.layer) self.moving_part_count = 0 if type == 'super': self.supermario() def shoe_move(self): if self.moving_part_count % 3 == 0: self.shoe.move(3, 0) elif self.moving_part_count % 3 == 1: self.shoe.move(-5,0) else: self.shoe.move(2,0) self.moving_part_count += 1 if self.moving_part_count % 3 == 0: self.moving_part_count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def supermario(self): tempPt = self.body.getReferencePoint() self.layer.adjustReference(tempPt.getX(), tempPt.getY()) for i in range(3): self.layer.scale(1.3) sleep(t/2) self.layer.scale(0.9) sleep(t/2) def walk(self,x=20): assert x > 0 total_step = int(x / self.step_size) for i in range(total_step): sleep(t/4) self.move(self.step_size, 0) self.shoe_move() def show_animation(): sleep(t) mario.move(0, -50) mushroom.arise() sleep(t) mario.move(0, 50) mushroom.move(0, 8) for i in range(7): sleep(t/2) mushroom.move(10, 0) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t/2) mushroom.move(0, 50) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t) mushroom.move(0, 50) sleep(t/2) mushroom.diappear() sleep(t/2) mario.supermario() for i in range(6): sleep(t/2) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() for i in range(2): sleep(t) mario.move(28, -60) for i in range(1): sleep(t) mario.move(32, 40) sleep(2*t) for i in range(4): sleep(t) mario.move(0, 25) def interactive_example(): while True: e = _scene.wait() d = e.getDescription() if d == "keyboard": k = e.getKey() if k == "q": _scene.close() break elif k == "w": mario.walk(20) elif k == "r": mario.walk(40) elif k == "j": mario.move(0, -50) sleep(t) mario.move(0, 50) create_world() mario = Mario('Blue', 'normal') mushroom = Mushroom(200, 92) show_animation() # interactive_example()
null
null
variable
layer() Contains eyes mouth wings body
cs1qa
null
null
null
null
null
Question: What is a bird??(What variable does it contain?) Code: from cs1graphics import * from time import sleep _scene = None _world = None t = 0.2 def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor('green') grass.setDepth(100) _scene.add(grass) #blocks block = Rectangle(40, 40, Point(200, 100)) block.setFillColor('brown') qmark = Text("?", 20, Point(200, 100)) qmark.setFontColor('Yellow') qmark.setDepth(48) _scene.add(qmark) block2 = block.clone() block2.move(40, 0) block.setDepth(50) _scene.add(block) _scene.add(block2) #pipe pipe = Polygon(Point(400, 150), Point(400, 160), Point(410, 160), Point(410, 320), Point(470, 320), Point(470, 160), Point(480, 160), Point(480, 150)) pipe.setFillColor('lightgreen') pipe.setDepth(10) pipe.move(-10, 0) _scene.add(pipe) class Mushroom(object): def __init__(self, x=200, y=92): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(lowermush) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom _scene.add(self.layer) def diappear(self): self.layer.scale(0.001) def move(self, x, y): self.layer.move(x, y) def arise(self): self.layer.setDepth(45) self.layer.move(0, -20) COLOR = ['Red', 'Blue'] TYPE = ['super', 'normal'] class Mario(object): def __init__(self, color='Blue', type='normal'): assert type in TYPE and color in COLOR self.color = color self.type = type self.step_size = 3 # Constructing Mario mario = Layer() # body body = Rectangle(33, 22, Point(200, 200)) body.setFillColor(color) body.setDepth(50) mario.add(body) # face face = Ellipse(30, 20, Point(200, 180)) face.setFillColor('beige') face.setDepth(40) mario.add(face) #hat hat = Polygon(Point(185, 175), Point(220, 175), Point(220, 173), Point(215, 173), Point(212, 168), Point(188, 168)) hat.setFillColor(color) hat.setDepth(39) mario.add(hat) #beard beard = Polygon(Point(207, 183), Point(217, 183), Point(215, 180), Point(209, 180)) beard.setFillColor('Brown') beard.setDepth(38) mario.add(beard) shoe = Layer() #left shoe lshoe = Rectangle(15, 6, Point(191, 215)) lshoe.setFillColor('black') lshoe.setDepth(52) shoe.add(lshoe) #right shoe rshoe = lshoe.clone() rshoe.move(17, 0) shoe.add(rshoe) mario.add(shoe) # save alias of moveable parts self.layer = mario self.body = body self.hat = hat self.shoe = shoe _scene.add(self.layer) self.moving_part_count = 0 if type == 'super': self.supermario() def shoe_move(self): if self.moving_part_count % 3 == 0: self.shoe.move(3, 0) elif self.moving_part_count % 3 == 1: self.shoe.move(-5,0) else: self.shoe.move(2,0) self.moving_part_count += 1 if self.moving_part_count % 3 == 0: self.moving_part_count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def supermario(self): tempPt = self.body.getReferencePoint() self.layer.adjustReference(tempPt.getX(), tempPt.getY()) for i in range(3): self.layer.scale(1.3) sleep(t/2) self.layer.scale(0.9) sleep(t/2) def walk(self,x=20): assert x > 0 total_step = int(x / self.step_size) for i in range(total_step): sleep(t/4) self.move(self.step_size, 0) self.shoe_move() def show_animation(): sleep(t) mario.move(0, -50) mushroom.arise() sleep(t) mario.move(0, 50) mushroom.move(0, 8) for i in range(7): sleep(t/2) mushroom.move(10, 0) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t/2) mushroom.move(0, 50) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t) mushroom.move(0, 50) sleep(t/2) mushroom.diappear() sleep(t/2) mario.supermario() for i in range(6): sleep(t/2) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() for i in range(2): sleep(t) mario.move(28, -60) for i in range(1): sleep(t) mario.move(32, 40) sleep(2*t) for i in range(4): sleep(t) mario.move(0, 25) def interactive_example(): while True: e = _scene.wait() d = e.getDescription() if d == "keyboard": k = e.getKey() if k == "q": _scene.close() break elif k == "w": mario.walk(20) elif k == "r": mario.walk(40) elif k == "j": mario.move(0, -50) sleep(t) mario.move(0, 50) create_world() mario = Mario('Blue', 'normal') mushroom = Mushroom(200, 92) show_animation() # interactive_example()
3
3
TA
task 4 Why did you choose the window judgment method that way?
from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace('blue') # Now close all the windows in the house! my_robot.move() my_robot.turn_left() my_robot.drop_beeper() my_robot.move() def turn_around(): for i in range(2): my_robot.turn_left() def turn_right(): for i in range(3): my_robot.turn_left() def drop_and_move(): while my_robot.front_is_clear() and not my_robot.on_beeper(): my_robot.move() if my_robot.left_is_clear() and not my_robot.on_beeper(): my_robot.move() if not my_robot.left_is_clear(): turn_around() my_robot.move() my_robot.drop_beeper() turn_around() my_robot.move() else: turn_around() my_robot.move() turn_right() turn_right() while not my_robot.on_beeper(): drop_and_move() my_robot.pick_beeper()
24
39
reasoning
The corner at the bottom right of the rain2 map should be recognized as not a window, but in this case, the left side is not blocked after going one more space.
cs1qa
null
null
null
null
null
Question: task 4 Why did you choose the window judgment method that way? Code: from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace('blue') # Now close all the windows in the house! my_robot.move() my_robot.turn_left() my_robot.drop_beeper() my_robot.move() def turn_around(): for i in range(2): my_robot.turn_left() def turn_right(): for i in range(3): my_robot.turn_left() def drop_and_move(): while my_robot.front_is_clear() and not my_robot.on_beeper(): my_robot.move() if my_robot.left_is_clear() and not my_robot.on_beeper(): my_robot.move() if not my_robot.left_is_clear(): turn_around() my_robot.move() my_robot.drop_beeper() turn_around() my_robot.move() else: turn_around() my_robot.move() turn_right() turn_right() while not my_robot.on_beeper(): drop_and_move() my_robot.pick_beeper()
3
3
TA
How did you separate the windows and walls in task4??
from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. hubo = Robot(beepers=100, avenue=2, street=6, orientation='E') hubo.set_trace('blue') def turn_around() : hubo.turn_left() hubo.turn_left() def turn_right() : turn_around() hubo.turn_left() hubo.move() hubo.drop_beeper() turn_right() hubo.move() m=0 k=0 while(not(hubo.on_beeper())): m=k k=0 if(hubo.right_is_clear()): k=1 if(k==0 and m==1): turn_around() hubo.move() hubo.drop_beeper() turn_around() hubo.move() if(k==1 and m==1): turn_around() hubo.move() hubo.turn_left() m=0 k=0 if(not(hubo.front_is_clear())): hubo.turn_left() hubo.move() # Now close all the windows in the house!
24
37
code_explain
If the right side is empty, it is basically a window. If the right side is empty twice in a row, I used a right turn, not a window. Whether the right side is empty in the previous column or m, and whether the right side is empty in this column is indicated through k
cs1qa
null
null
null
null
null
Question: How did you separate the windows and walls in task4?? Code: from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. hubo = Robot(beepers=100, avenue=2, street=6, orientation='E') hubo.set_trace('blue') def turn_around() : hubo.turn_left() hubo.turn_left() def turn_right() : turn_around() hubo.turn_left() hubo.move() hubo.drop_beeper() turn_right() hubo.move() m=0 k=0 while(not(hubo.on_beeper())): m=k k=0 if(hubo.right_is_clear()): k=1 if(k==0 and m==1): turn_around() hubo.move() hubo.drop_beeper() turn_around() hubo.move() if(k==1 and m==1): turn_around() hubo.move() hubo.turn_left() m=0 k=0 if(not(hubo.front_is_clear())): hubo.turn_left() hubo.move() # Now close all the windows in the house!
3
3
TA
And secondly, it would be nice to briefly explain the algorithm you used when implementing Task4 Rain code.
from cs1robots import * # Your code must work for both of the worlds below. # load_world('worlds/rain1.wld') load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. # Now close all the windows in the house! def move(n=1): for i in range(n): h.move() def turn(n): for i in range(n): h.turn_left() def is_window(): move() flag_wall = h.right_is_clear() move() flag_window = h.right_is_clear() turn(2) move(2) turn(2) return flag_wall and (not flag_window) h = Robot(beepers=100, avenue=2, street=6, orientation='E') h.set_trace('blue') move() turn(3) h.drop_beeper() move() while True: if h.on_beeper(): h.pick_beeper() break elif (not h.front_is_clear()): turn(1) elif h.right_is_clear(): move() is_window = (not h.right_is_clear()) turn(2) move() turn(2) if is_window: h.drop_beeper() move() else: turn(3) move() else: move() turn(3)
19
60
code_explain
And in the case of task4, the inside of the house was turned counterclockwise, and if the right side is empty, it checks whether it is a wall in front of a corner or a window.
cs1qa
null
null
null
null
null
Question: And secondly, it would be nice to briefly explain the algorithm you used when implementing Task4 Rain code. Code: from cs1robots import * # Your code must work for both of the worlds below. # load_world('worlds/rain1.wld') load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. # Now close all the windows in the house! def move(n=1): for i in range(n): h.move() def turn(n): for i in range(n): h.turn_left() def is_window(): move() flag_wall = h.right_is_clear() move() flag_window = h.right_is_clear() turn(2) move(2) turn(2) return flag_wall and (not flag_window) h = Robot(beepers=100, avenue=2, street=6, orientation='E') h.set_trace('blue') move() turn(3) h.drop_beeper() move() while True: if h.on_beeper(): h.pick_beeper() break elif (not h.front_is_clear()): turn(1) elif h.right_is_clear(): move() is_window = (not h.right_is_clear()) turn(2) move() turn(2) if is_window: h.drop_beeper() move() else: turn(3) move() else: move() turn(3)
3
3
TA
task 4 Why did you choose the window judgment method that way?
from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace('blue') # Now close all the windows in the house! my_robot.move() my_robot.turn_left() my_robot.drop_beeper() my_robot.move() def turn_around(): for i in range(2): my_robot.turn_left() def turn_right(): for i in range(3): my_robot.turn_left() def drop_and_move(): while my_robot.front_is_clear() and not my_robot.on_beeper(): my_robot.move() if my_robot.left_is_clear() and not my_robot.on_beeper(): my_robot.move() if not my_robot.left_is_clear(): turn_around() my_robot.move() my_robot.drop_beeper() turn_around() my_robot.move() else: turn_around() my_robot.move() turn_right() turn_right() while not my_robot.on_beeper(): drop_and_move() my_robot.pick_beeper()
24
39
reasoning
The corner at the bottom right of the rain2 map should be recognized as not a window, but in this case, the left side is not blocked after going one more space.
cs1qa
null
null
null
null
null
Question: task 4 Why did you choose the window judgment method that way? Code: from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace('blue') # Now close all the windows in the house! my_robot.move() my_robot.turn_left() my_robot.drop_beeper() my_robot.move() def turn_around(): for i in range(2): my_robot.turn_left() def turn_right(): for i in range(3): my_robot.turn_left() def drop_and_move(): while my_robot.front_is_clear() and not my_robot.on_beeper(): my_robot.move() if my_robot.left_is_clear() and not my_robot.on_beeper(): my_robot.move() if not my_robot.left_is_clear(): turn_around() my_robot.move() my_robot.drop_beeper() turn_around() my_robot.move() else: turn_around() my_robot.move() turn_right() turn_right() while not my_robot.on_beeper(): drop_and_move() my_robot.pick_beeper()
3
0
TA
Please explain how you used the loop in #1 to solve the problem!
from cs1robots import * create_world() r=Robot(orientation='W', avenue=7, street=5) r.set_trace('red') while not r.facing_north(): r.turn_left() r.turn_left() while r.front_is_clear(): r.move() r.turn_left() while r.front_is_clear(): r.move() r.turn_left()
8
21
code_explain
The first loop was not given an initial direction, so the robot turned left until it looked north. The second and third loops were made to move forward just before the front was blocked by a wall.
cs1qa
null
null
null
null
null
Question: Please explain how you used the loop in #1 to solve the problem! Code: from cs1robots import * create_world() r=Robot(orientation='W', avenue=7, street=5) r.set_trace('red') while not r.facing_north(): r.turn_left() r.turn_left() while r.front_is_clear(): r.move() r.turn_left() while r.front_is_clear(): r.move() r.turn_left()
6
0
TA
Could you please explain what append does in Task 1?
def fibonacci(upper_bound): sequence=[0,1] n=0 while (sequence[n]+sequence[n+1])<1000: sequence.append(sequence[n]+sequence[n+1]) n+=1 return sequence print(fibonacci(1000))
4
4
variable
The append function is a function that adds an element to the end of the list. It serves to add a new Fibonacci number to the list to be returned.
cs1qa
null
null
null
null
null
Question: Could you please explain what append does in Task 1? Code: def fibonacci(upper_bound): sequence=[0,1] n=0 while (sequence[n]+sequence[n+1])<1000: sequence.append(sequence[n]+sequence[n+1]) n+=1 return sequence print(fibonacci(1000))
9
1
TA
Please explain how you implemented the draw_card of task 2
import random from cs1graphics import * path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') ### class Card(object): state=True ### def create_deck(number = 1): deck=[] for i in range(len(suit_names)): for j in range(len(face_names)): c=Card() c.suit=suit_names[i] c.face=face_names[j] c.value=value[j] c.image=Image(path+suit_names[i]+'_'+face_names[j]+'.png') deck.append(c) random.shuffle(deck) return deck def hand_value(hand): total=0 for i in range(len(hand)): total += hand[i].value return total def card_string(card): article='a ' if card.face in ['8','Ace']: article='an ' return (article+card.face+" of "+card.suit) def ask_yesno(prompt): while True: a=input(prompt,) if a=='y': return True elif a=='n': return False else: print('I beg your pardon!') continue def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for i in range(len(dealer)): if dealer[i].state==False: img=Image(path+'Back.png') else: img=Image(path+dealer[i].suit+'_'+dealer[i].face+'.png') img.moveTo(x0+i*10,y0) img.setDepth(depth-i*10) bj_board.add(img) for i in range(len(player)): img=Image(path+player[i].suit+'_'+player[i].face+'.png') img.moveTo(x1+i*10,y1) img.setDepth(depth-i*10) bj_board.add(img) t_d=Text("The dealer's Total : "+str(hand_value(dealer))) t_p=Text("The player's Total : "+str(hand_value(player))) t_d.moveTo(450,100) t_p.moveTo(450,300) if dealer[0].state: bj_board.add(t_d) bj_board.add(t_p) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
77
121
code_explain
Using the for statement as much as the card length of the dealer and player, an image was added to the board with a card spacing of 10 and a depth difference of 10.
cs1qa
null
null
null
null
null
Question: Please explain how you implemented the draw_card of task 2 Code: import random from cs1graphics import * path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') ### class Card(object): state=True ### def create_deck(number = 1): deck=[] for i in range(len(suit_names)): for j in range(len(face_names)): c=Card() c.suit=suit_names[i] c.face=face_names[j] c.value=value[j] c.image=Image(path+suit_names[i]+'_'+face_names[j]+'.png') deck.append(c) random.shuffle(deck) return deck def hand_value(hand): total=0 for i in range(len(hand)): total += hand[i].value return total def card_string(card): article='a ' if card.face in ['8','Ace']: article='an ' return (article+card.face+" of "+card.suit) def ask_yesno(prompt): while True: a=input(prompt,) if a=='y': return True elif a=='n': return False else: print('I beg your pardon!') continue def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for i in range(len(dealer)): if dealer[i].state==False: img=Image(path+'Back.png') else: img=Image(path+dealer[i].suit+'_'+dealer[i].face+'.png') img.moveTo(x0+i*10,y0) img.setDepth(depth-i*10) bj_board.add(img) for i in range(len(player)): img=Image(path+player[i].suit+'_'+player[i].face+'.png') img.moveTo(x1+i*10,y1) img.setDepth(depth-i*10) bj_board.add(img) t_d=Text("The dealer's Total : "+str(hand_value(dealer))) t_p=Text("The player's Total : "+str(hand_value(player))) t_d.moveTo(450,100) t_p.moveTo(450,300) if dealer[0].state: bj_board.add(t_d) bj_board.add(t_p) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
5
0
TA
Yes, please explain task 1!
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass global balance balance=balance+money ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! pass global balance if money<=balance: balance=balance-money else: print('But you only have ',balance,' won') ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass if process == '': return elif process == 'd': add=input("How much do you want to deposit?") print('You deposited ',add,' won') ad=int(add) deposit(ad) elif process == 'w': take=input("How much do you want to withdraw?") print("You've withdrawed ",take," won") tak=int(take) withdrawal(tak) elif process == 'c': print('Your current balance is ',balance,' won') else: print('Please, press d or w or c or return') ################# bank()
0
72
code_explain
Case 1 I changed the value of the variable balance using global Decide which function to execute using the input function In the case of deposit and withdrawal, the amount of change was also input and applied to the balance. If '' is inputted, the process is finished using the return function.
cs1qa
null
null
null
null
null
Question: Yes, please explain task 1! Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass global balance balance=balance+money ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! pass global balance if money<=balance: balance=balance-money else: print('But you only have ',balance,' won') ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass if process == '': return elif process == 'd': add=input("How much do you want to deposit?") print('You deposited ',add,' won') ad=int(add) deposit(ad) elif process == 'w': take=input("How much do you want to withdraw?") print("You've withdrawed ",take," won") tak=int(take) withdrawal(tak) elif process == 'c': print('Your current balance is ',balance,' won') else: print('Please, press d or w or c or return') ################# bank()
4
0
TA
Task 1. Please explain what are l, k, s, respectively
from cs1robots import * load_world("worlds/add34.wld") hubo=Robot() # hubo.set_trace("blue") # hubo.set_pause(1) hubo.turn_left() hubo.move() def turn_right(): for i in range (3): hubo.turn_left() turn_right() while hubo.front_is_clear(): hubo.move() k=0 l=0 s = 0 while True: while hubo.on_beeper(): hubo.pick_beeper() k+=1 turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() l+=1 if l+k+s>9: for i in range ((l+k+s)%10): hubo.drop_beeper() s = (l+k+s)//10 l = 0 k = 0 else: for i in range (l+k+s): hubo.drop_beeper() k=l=s=0 if(not hubo.right_is_clear()): break turn_right() hubo.move() turn_right() hubo.move() turn_right()
28
30
variable
k is the value of beeper on the first line, l is the value of beeper on the second line and s is 1 or 0 depending on does l+k more than 9 or not
cs1qa
null
null
null
null
null
Question: Task 1. Please explain what are l, k, s, respectively Code: from cs1robots import * load_world("worlds/add34.wld") hubo=Robot() # hubo.set_trace("blue") # hubo.set_pause(1) hubo.turn_left() hubo.move() def turn_right(): for i in range (3): hubo.turn_left() turn_right() while hubo.front_is_clear(): hubo.move() k=0 l=0 s = 0 while True: while hubo.on_beeper(): hubo.pick_beeper() k+=1 turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() l+=1 if l+k+s>9: for i in range ((l+k+s)%10): hubo.drop_beeper() s = (l+k+s)//10 l = 0 k = 0 else: for i in range (l+k+s): hubo.drop_beeper() k=l=s=0 if(not hubo.right_is_clear()): break turn_right() hubo.move() turn_right() hubo.move() turn_right()
1
0
TA
Could you please explain what functions hubo_run() and hubo_round() are respectively?
from cs1robots import* create_world() hubo=Robot() def hubo_run(): for i in range(9): hubo.move() def hubo_round(): for i in range(3): hubo.turn_left() hubo.turn_left() hubo.set_trace('blue') hubo_run() hubo_round() hubo.move() hubo_round() hubo_run() hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() hubo_run() hubo_round() hubo.move() hubo_round() hubo_run() hubo.turn_left() hubo.move() hubo.turn_left() hubo_run() hubo_round() hubo.move() hubo_round() hubo_run()
5
10
variable
We set this as a function because it repeats moving forward by 9 spaces, and we set each as a function because we always take the action of turning left at the vertex 3 times.
cs1qa
null
null
null
null
null
Question: Could you please explain what functions hubo_run() and hubo_round() are respectively? Code: from cs1robots import* create_world() hubo=Robot() def hubo_run(): for i in range(9): hubo.move() def hubo_round(): for i in range(3): hubo.turn_left() hubo.turn_left() hubo.set_trace('blue') hubo_run() hubo_round() hubo.move() hubo_round() hubo_run() hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() hubo_run() hubo_round() hubo.move() hubo_round() hubo_run() hubo.turn_left() hubo.move() hubo.turn_left() hubo_run() hubo_round() hubo.move() hubo_round() hubo_run()
4
0
TA
Briefly explain the name and action of the function you add in Task1!
from cs1robots import * load_world('worlds/add34.wld') my_robot = Robot() ''' Abstract, Final, Static class "Logic". Contains useful logics, which are frequently used. Since I started programming with Java, "global functions" do not look so good for me :D ''' class Logic: @staticmethod def rpt(fu, i, args=tuple()): #RePeaT for cnt in range(i): fu(*args) @staticmethod def rptl(fu, i, args=tuple()): #RePeaT, but check Last run for cnt in range(i): fu(*args, last = (cnt == i - 1)) @staticmethod def dw(condition, task = lambda : None): #Do While condition is true cnt = 0 while condition(): task() cnt -= -1 return cnt @staticmethod def until(condition, task = lambda : None): #do UNTIL condition is true cnt = 0 while not condition(): task() cnt -= -1 return cnt @staticmethod def dt(*tasks): #Do Tasks for task in tasks: task() ''' RobotController has-a Robot. ''' class RobotController: def __init__(self, robot): self.rb = robot self.setup_commands() def mv(self, i = 1): Logic.rpt(self.rb.move, i) ''' For who feel like "tl; dr:" start with... "t" -> turn "c" -> is it clear? "b" -> beeper work ''' def setup_commands(self): self.tl = self.rb.turn_left #Turn Left self.tr = lambda : Logic.rpt(self.tl, 3) #Turn Right self.tb = lambda : Logic.rpt(self.tl, 2) #Turn Back self.tn = lambda : Logic.until(self.rb.facing_north, self.tl) #Turn North self.tw = lambda : Logic.dt(self.tn, self.tl) self.ts = lambda : Logic.dt(self.tn, self.tb) self.te = lambda : Logic.dt(self.tn, self.tr) # coding at almost 3:00 AM... My life is legend self.cf = self.rb.front_is_clear #Clear Front self.cl = self.rb.left_is_clear #Clear Left self.cr = self.rb.right_is_clear #Clear Right self.bo = self.rb.on_beeper #Beeper On? self.bp = self.rb.pick_beeper #Beeper Pick self.bd = self.rb.drop_beeper #Beeper Drop self.bc = self.rb.carries_beepers #Beeper Carries? self.st = self.rb.set_trace self.mw = lambda : Logic.dw(self.cf, self.mv) #Move to Wall def setup_self(self): Logic.dt(self.mw, self.tl, self.mv, self.tl) def count_beeps(self): return Logic.dw(self.bo, self.bp) def add_digit(self, addition): foo = addition + self.count_beeps() Logic.dt(self.tl, self.mv) foo += self.count_beeps() Logic.rpt(self.bd, (foo%10)) Logic.dt(self.tb, self.mv, self.tl) return foo//10 def go_to_end_point(self): Logic.dt(self.tl, self.mv, self.tl) Logic.until(self.bo, self.mv) Logic.dt(self.tb, self.mv) def solve_assignment(self): self.setup_self() bar = 0 while self.cf(): bar = self.add_digit(bar) if self.cf(): self.mv() self.go_to_end_point() robot_control = RobotController(my_robot) robot_control.solve_assignment()
124
131
variable
solve_assignment. It repeats add_digit that adds the number of digits to the currently standing column, and performs retrieval and map end processing.
cs1qa
null
null
null
null
null
Question: Briefly explain the name and action of the function you add in Task1! Code: from cs1robots import * load_world('worlds/add34.wld') my_robot = Robot() ''' Abstract, Final, Static class "Logic". Contains useful logics, which are frequently used. Since I started programming with Java, "global functions" do not look so good for me :D ''' class Logic: @staticmethod def rpt(fu, i, args=tuple()): #RePeaT for cnt in range(i): fu(*args) @staticmethod def rptl(fu, i, args=tuple()): #RePeaT, but check Last run for cnt in range(i): fu(*args, last = (cnt == i - 1)) @staticmethod def dw(condition, task = lambda : None): #Do While condition is true cnt = 0 while condition(): task() cnt -= -1 return cnt @staticmethod def until(condition, task = lambda : None): #do UNTIL condition is true cnt = 0 while not condition(): task() cnt -= -1 return cnt @staticmethod def dt(*tasks): #Do Tasks for task in tasks: task() ''' RobotController has-a Robot. ''' class RobotController: def __init__(self, robot): self.rb = robot self.setup_commands() def mv(self, i = 1): Logic.rpt(self.rb.move, i) ''' For who feel like "tl; dr:" start with... "t" -> turn "c" -> is it clear? "b" -> beeper work ''' def setup_commands(self): self.tl = self.rb.turn_left #Turn Left self.tr = lambda : Logic.rpt(self.tl, 3) #Turn Right self.tb = lambda : Logic.rpt(self.tl, 2) #Turn Back self.tn = lambda : Logic.until(self.rb.facing_north, self.tl) #Turn North self.tw = lambda : Logic.dt(self.tn, self.tl) self.ts = lambda : Logic.dt(self.tn, self.tb) self.te = lambda : Logic.dt(self.tn, self.tr) # coding at almost 3:00 AM... My life is legend self.cf = self.rb.front_is_clear #Clear Front self.cl = self.rb.left_is_clear #Clear Left self.cr = self.rb.right_is_clear #Clear Right self.bo = self.rb.on_beeper #Beeper On? self.bp = self.rb.pick_beeper #Beeper Pick self.bd = self.rb.drop_beeper #Beeper Drop self.bc = self.rb.carries_beepers #Beeper Carries? self.st = self.rb.set_trace self.mw = lambda : Logic.dw(self.cf, self.mv) #Move to Wall def setup_self(self): Logic.dt(self.mw, self.tl, self.mv, self.tl) def count_beeps(self): return Logic.dw(self.bo, self.bp) def add_digit(self, addition): foo = addition + self.count_beeps() Logic.dt(self.tl, self.mv) foo += self.count_beeps() Logic.rpt(self.bd, (foo%10)) Logic.dt(self.tb, self.mv, self.tl) return foo//10 def go_to_end_point(self): Logic.dt(self.tl, self.mv, self.tl) Logic.until(self.bo, self.mv) Logic.dt(self.tb, self.mv) def solve_assignment(self): self.setup_self() bar = 0 while self.cf(): bar = self.add_digit(bar) if self.cf(): self.mv() self.go_to_end_point() robot_control = RobotController(my_robot) robot_control.solve_assignment()
1
4
TA
Finally, I'll ask you to explain task5.
from cs1robots import* load_world('worlds/harvest2.wld') hubo=Robot(beepers=36) for i in range(5): hubo.move() hubo.turn_left() hubo.move() def turn_right(): for i in range(3): hubo.turn_left() for i in range(6): for j in range(5): hubo.pick_beeper() turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() if i%2==0: for k in range(2): hubo.move() hubo.turn_left() else: if i<5: for k in range(2): turn_right() hubo.move()
0
30
code_explain
Task5 seems to have multiple answers. I’m going to take the process of picking up the beeper in the diagonal direction I thought I could repeat it 6 times I used a loop Depending on the condition of i, the process of moving to the next diagonal I think it should be a little different, so only that part I used conditional sentences
cs1qa
null
null
null
null
null
Question: Finally, I'll ask you to explain task5. Code: from cs1robots import* load_world('worlds/harvest2.wld') hubo=Robot(beepers=36) for i in range(5): hubo.move() hubo.turn_left() hubo.move() def turn_right(): for i in range(3): hubo.turn_left() for i in range(6): for j in range(5): hubo.pick_beeper() turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() if i%2==0: for k in range(2): hubo.move() hubo.turn_left() else: if i<5: for k in range(2): turn_right() hubo.move()
5
0
TA
Is there any reason I used if-pass in task1??
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# if money>0: pass print("You deposited " + str(money) + " won") ################# global balance balance+=money def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance # Withdraw the money from the current balance if balance>=money: pass else: print ("You've withdrawn " +str(money)+ " won") print("But you only have " +str(balance)+ " won") return print("You've withdrawn " +str(money)+ " won") balance-=money ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. if process=='d'or process=='w'or process=='c': pass elif process=='': return else: print('Please, press d or w or c or return') if process=='d': money=int(input("How much do you want to deposit?:")) deposit(money) elif process=='w': money=int(input("How much do you want to withdraw?:")) withdrawal(money) elif process=='c': print ("Your current balnce is " +str(balance)+ " won") ################# bank()
46
47
reasoning
I don't think I need it
cs1qa
null
null
null
null
null
Question: Is there any reason I used if-pass in task1?? Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# if money>0: pass print("You deposited " + str(money) + " won") ################# global balance balance+=money def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance # Withdraw the money from the current balance if balance>=money: pass else: print ("You've withdrawn " +str(money)+ " won") print("But you only have " +str(balance)+ " won") return print("You've withdrawn " +str(money)+ " won") balance-=money ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. if process=='d'or process=='w'or process=='c': pass elif process=='': return else: print('Please, press d or w or c or return') if process=='d': money=int(input("How much do you want to deposit?:")) deposit(money) elif process=='w': money=int(input("How much do you want to withdraw?:")) withdrawal(money) elif process=='c': print ("Your current balnce is " +str(balance)+ " won") ################# bank()
1
2
TA
Can you explain the climb_up(n) function??
import cs1robots as cr cr.load_world("worlds/newspaper.wld") hubo=cr.Robot(beepers=1) def turn_right(): for i in range(3): hubo.turn_left() def climb_up(n): for i in range(n): hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.move() def climb_down(n): for i in range(n): hubo.move() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() climb_up(4) hubo.drop_beeper() hubo.turn_left() hubo.turn_left() climb_down(4) hubo.move()
9
16
variable
The climb_up(n) function implies that the robot climbs the stairs n times. When I was right in front of the chin on the stairs
cs1qa
null
null
null
null
null
Question: Can you explain the climb_up(n) function?? Code: import cs1robots as cr cr.load_world("worlds/newspaper.wld") hubo=cr.Robot(beepers=1) def turn_right(): for i in range(3): hubo.turn_left() def climb_up(n): for i in range(n): hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.move() def climb_down(n): for i in range(n): hubo.move() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() climb_up(4) hubo.drop_beeper() hubo.turn_left() hubo.turn_left() climb_down(4) hubo.move()
6
2
TA
Please explain the is_valid function in step 3
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list copy = cards.copy() cards.clear() for i in range(24): a = random.randrange(0, i+1) cards.insert(a, copy[i]) ################################################################ canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(5) def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list : # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 in correct_list or num2 in correct_list: return True elif num1 == num2: return True elif num1 < 0 or num2 < 0 or num1 > 23 or num2 > 23: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### cards[num1][0].moveTo(100*(num1%6) + 70, 130*(num1//6) + 90) canvas.add(cards[num1][0]) cards[num2][0].moveTo(100*(num2%6) + 70, 130*(num2//6) + 90) canvas.add(cards[num2][0]) time.sleep(1) if cards[num1][1] == cards[num2][1]: correct_list.append(num1) correct_list.append(num2) print_cards() return True print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### tries = tries + 1# 3-2-5. Update number of tries (global variable, tries) ###########################################################################
78
91
variable
The is_valid() function determines whether num1 and num2 are in the correct_list or is the same, or whether they are numbers in the correct range, and only then advances to the next.
cs1qa
null
null
null
null
null
Question: Please explain the is_valid function in step 3 Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list copy = cards.copy() cards.clear() for i in range(24): a = random.randrange(0, i+1) cards.insert(a, copy[i]) ################################################################ canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(5) def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list : # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 in correct_list or num2 in correct_list: return True elif num1 == num2: return True elif num1 < 0 or num2 < 0 or num1 > 23 or num2 > 23: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### cards[num1][0].moveTo(100*(num1%6) + 70, 130*(num1//6) + 90) canvas.add(cards[num1][0]) cards[num2][0].moveTo(100*(num2%6) + 70, 130*(num2//6) + 90) canvas.add(cards[num2][0]) time.sleep(1) if cards[num1][1] == cards[num2][1]: correct_list.append(num1) correct_list.append(num2) print_cards() return True print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### tries = tries + 1# 3-2-5. Update number of tries (global variable, tries) ###########################################################################
1
4
TA
In task 5, the range was set to (5, 0, -2) in the for statement. Please explain how to write the path using this.
from cs1robots import * import time load_world('worlds/harvest2.wld') hubo = Robot(beepers=25) sleep_time = 0.1 def turn_right(): for i in range(3): hubo.turn_left() def pick(): if hubo.on_beeper(): time.sleep(sleep_time) hubo.pick_beeper() for i in range(5): hubo.move() for i in range(5, 0, -2): for j in range(i): hubo.turn_left() time.sleep(sleep_time) hubo.move() pick() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() for j in range(i): time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() time.sleep(sleep_time) hubo.move() turn_right() hubo.move() hubo.turn_left() for j in range(i): pick() time.sleep(sleep_time) hubo.move() hubo.turn_left() time.sleep(sleep_time) hubo.move() turn_right() hubo.turn_left() hubo.turn_left() for j in range(i-1): pick() time.sleep(sleep_time) hubo.move() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() hubo.move()
19
63
code_explain
Using the method of picking up beepers like a snail shell turns, I picked up the beeper while spinning while eating 5 for the first wheel and 3 for the second.
cs1qa
null
null
null
null
null
Question: In task 5, the range was set to (5, 0, -2) in the for statement. Please explain how to write the path using this. Code: from cs1robots import * import time load_world('worlds/harvest2.wld') hubo = Robot(beepers=25) sleep_time = 0.1 def turn_right(): for i in range(3): hubo.turn_left() def pick(): if hubo.on_beeper(): time.sleep(sleep_time) hubo.pick_beeper() for i in range(5): hubo.move() for i in range(5, 0, -2): for j in range(i): hubo.turn_left() time.sleep(sleep_time) hubo.move() pick() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() for j in range(i): time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() time.sleep(sleep_time) hubo.move() turn_right() hubo.move() hubo.turn_left() for j in range(i): pick() time.sleep(sleep_time) hubo.move() hubo.turn_left() time.sleep(sleep_time) hubo.move() turn_right() hubo.turn_left() hubo.turn_left() for j in range(i-1): pick() time.sleep(sleep_time) hubo.move() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() hubo.move()
6
2
TA
Why is the condition like that in print_cards?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if(0 <= num1 <= 23 and 0 <= num2 <= 23 and num1 != num2 and not num1 in correct_list and not num2 in correct_list): return True ########################################################################### return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. correct_list.append(num1) correct_list.append(num2) print_cards() time.sleep(1) if(cards[num1][1] == cards[num2][1]): return True else: correct_list.remove(num1) correct_list.remove(num2) ########################################################################### print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while (len(correct_list) != 24): # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if(tries%10==1): print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==2): print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==3): print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") tries = tries+1 ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
45
45
reasoning
This is because cards with numbers in the correct list must be turned over, cards with numbers that are not in the correct list must be turned over.
cs1qa
null
null
null
null
null
Question: Why is the condition like that in print_cards? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if(0 <= num1 <= 23 and 0 <= num2 <= 23 and num1 != num2 and not num1 in correct_list and not num2 in correct_list): return True ########################################################################### return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. correct_list.append(num1) correct_list.append(num2) print_cards() time.sleep(1) if(cards[num1][1] == cards[num2][1]): return True else: correct_list.remove(num1) correct_list.remove(num2) ########################################################################### print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while (len(correct_list) != 24): # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if(tries%10==1): print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==2): print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif(tries%10==3): print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") tries = tries+1 ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
3
2
TA
task3 You're very neat, is there a reason you didn't put 44-47 in the for statement??
from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo= Robot() hubo.set_trace('blue') def pick_all(): while hubo.on_beeper(): hubo.pick_beeper() def pick_go(): pick_all() while hubo.front_is_clear(): hubo.move() pick_all() def north(): while not hubo.facing_north(): hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def go(): while hubo.front_is_clear(): hubo.move() def starting(): north() for i in range(2): hubo.turn_left() go() hubo.turn_left() def zig(): pick_go() if hubo.right_is_clear(): turn_right() hubo.move() def zag(): pick_go() if hubo.left_is_clear(): hubo.turn_left() hubo.move() hubo.turn_left() zig() turn_right() zag() hubo.turn_left() while hubo.front_is_clear(): zig() turn_right() zag() hubo.turn_left() starting() while hubo.carries_beepers(): hubo.drop_beeper()
42
54
reasoning
It’s just because I didn’t put it in the zigzag assignment I did last time. It runs fine even if I just erase it
cs1qa
null
null
null
null
null
Question: task3 You're very neat, is there a reason you didn't put 44-47 in the for statement?? Code: from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo= Robot() hubo.set_trace('blue') def pick_all(): while hubo.on_beeper(): hubo.pick_beeper() def pick_go(): pick_all() while hubo.front_is_clear(): hubo.move() pick_all() def north(): while not hubo.facing_north(): hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def go(): while hubo.front_is_clear(): hubo.move() def starting(): north() for i in range(2): hubo.turn_left() go() hubo.turn_left() def zig(): pick_go() if hubo.right_is_clear(): turn_right() hubo.move() def zag(): pick_go() if hubo.left_is_clear(): hubo.turn_left() hubo.move() hubo.turn_left() zig() turn_right() zag() hubo.turn_left() while hubo.front_is_clear(): zig() turn_right() zag() hubo.turn_left() starting() while hubo.carries_beepers(): hubo.drop_beeper()
3
3
TA
Yes, simply explain closing_door!
from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') # Now close all the windows in the house! my_robot.set_pause(.1) def turn_right(): for i in range(3): my_robot.turn_left() def closing_door(): if my_robot.left_is_clear(): my_robot.drop_beeper() my_robot.move() if my_robot.left_is_clear(): turn_right() turn_right() my_robot.move() my_robot.pick_beeper() turn_right() my_robot.move() else : my_robot.move() my_robot.move() my_robot.drop_beeper() my_robot.turn_left() my_robot.move() while not my_robot.on_beeper(): if my_robot.front_is_clear(): closing_door() else : turn_right() my_robot.pick_beeper() my_robot.turn_left()
15
27
variable
Because all the windows are 1 space If the left side is empty, drop the beeper Even after moving forward, if it is empty, it is judged as a corner, not a window. Come back and retrieve the beeper and turn Or keep going It’s the function set up like that
cs1qa
null
null
null
null
null
Question: Yes, simply explain closing_door! Code: from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') # Now close all the windows in the house! my_robot.set_pause(.1) def turn_right(): for i in range(3): my_robot.turn_left() def closing_door(): if my_robot.left_is_clear(): my_robot.drop_beeper() my_robot.move() if my_robot.left_is_clear(): turn_right() turn_right() my_robot.move() my_robot.pick_beeper() turn_right() my_robot.move() else : my_robot.move() my_robot.move() my_robot.drop_beeper() my_robot.turn_left() my_robot.move() while not my_robot.on_beeper(): if my_robot.front_is_clear(): closing_door() else : turn_right() my_robot.pick_beeper() my_robot.turn_left()
1
0
TA
Please explain the zigzag function of task1.
from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def go_straight(): for i in range(9): hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def zigzag(): hubo.turn_left() go_straight() turn_right() hubo.move() turn_right() go_straight() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
10
16
variable
In order to use the for statement for the robot, the zigzag function defines a zigzag function that rotates left, goes up, goes right, goes forward once, goes right, goes down again, and descends back to the last line.
cs1qa
null
null
null
null
null
Question: Please explain the zigzag function of task1. Code: from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def go_straight(): for i in range(9): hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def zigzag(): hubo.turn_left() go_straight() turn_right() hubo.move() turn_right() go_straight() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
5
1
TA
Please explain how you used the layer in task 2 and the movement of each shape you created.
from cs1graphics import * from time import sleep from math import sin, cos,pi def draw_animal(): # Implement this function. nature = Canvas(1000,600) nature.setBackgroundColor('skyblue') global Olaf Olaf = Layer() # Form Olaf's Body# def create_body(): cir=Circle(50,Point(0,50)) cir.setFillColor('white') cir.setBorderColor('white') return cir top_body=create_body() bottom_body=create_body() bottom_body.scale(1.3) bottom_body.moveTo(0,-60) def create_arm(): ceta=40*2*pi/180 arm=Rectangle(3,40,Point(85*(cos(ceta)), -60+85*(sin(ceta)))) arm.setFillColor('brown') arm.setBorderColor('brown') arm.rotate(30) return arm global arm1 global arm2 arm1=create_arm() arm1.moveTo(-60,-10) arm2=create_arm() arm2.moveTo(60,-10) arm2.flip(0) def create_eye(): eye = Circle(5,Point(-20,60)) eye.setFillColor('black') eye.setDepth(0) return eye eye1= create_eye() eye2= create_eye() eye2.moveTo(20,60) Olaf.add(top_body) Olaf.add(bottom_body) Olaf.add(arm1) Olaf.add(arm2) Olaf.add(eye1) Olaf.add(eye2) Olaf.moveTo(200,400) Olaf.rotate(180) nature.add(Olaf) pass def show_animation(): # Implement this function. def Olaf_arm(t): degree=(t/45)*2*pi arm1.rotate(-degree) arm2.rotate(degree) def Olaf_Move(): Olaf.move(2,0) sleep(0.01) for t in range(185): Olaf_arm(t) Olaf_Move() pass draw_animal() show_animation()
0
76
code_explain
After creating the snowman's body, arms, and eyes, and adding them to the layer, I used the Layer to move the layer so that the body, arms, and eyes all move at once, and rotate was used to make the snowman move and the arms rotate.
cs1qa
null
null
null
null
null
Question: Please explain how you used the layer in task 2 and the movement of each shape you created. Code: from cs1graphics import * from time import sleep from math import sin, cos,pi def draw_animal(): # Implement this function. nature = Canvas(1000,600) nature.setBackgroundColor('skyblue') global Olaf Olaf = Layer() # Form Olaf's Body# def create_body(): cir=Circle(50,Point(0,50)) cir.setFillColor('white') cir.setBorderColor('white') return cir top_body=create_body() bottom_body=create_body() bottom_body.scale(1.3) bottom_body.moveTo(0,-60) def create_arm(): ceta=40*2*pi/180 arm=Rectangle(3,40,Point(85*(cos(ceta)), -60+85*(sin(ceta)))) arm.setFillColor('brown') arm.setBorderColor('brown') arm.rotate(30) return arm global arm1 global arm2 arm1=create_arm() arm1.moveTo(-60,-10) arm2=create_arm() arm2.moveTo(60,-10) arm2.flip(0) def create_eye(): eye = Circle(5,Point(-20,60)) eye.setFillColor('black') eye.setDepth(0) return eye eye1= create_eye() eye2= create_eye() eye2.moveTo(20,60) Olaf.add(top_body) Olaf.add(bottom_body) Olaf.add(arm1) Olaf.add(arm2) Olaf.add(eye1) Olaf.add(eye2) Olaf.moveTo(200,400) Olaf.rotate(180) nature.add(Olaf) pass def show_animation(): # Implement this function. def Olaf_arm(t): degree=(t/45)*2*pi arm1.rotate(-degree) arm2.rotate(degree) def Olaf_Move(): Olaf.move(2,0) sleep(0.01) for t in range(185): Olaf_arm(t) Olaf_Move() pass draw_animal() show_animation()
5
0
TA
What is the function of the global keyword in Task 1?
balance = 0 def deposit(money): # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance print('You deposited %d won' % money) global balance balance=balance+money # Do something on here ! def withdrawal(money): # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance print("You've withdrawn %d won" % money) if balance < money: print('But you only have %d won' % balance) return else: balance=balance-money # Do something on here ! def bank(): # Input : (None) No Input # Output : (None) No Output while True: global balance process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': return elif process=='d': money=input('How much do you want to deposit?') deposit(int(money)) elif process=='w': money=input('How much do you want to withdraw?') withdrawal(int(money)) elif process=='c': print('Your current balance is %d won' % balance) else : print('Please press d or w or c or return') bank()
0
53
variable
Even if a variable has a value by operating within a function, it is impossible to use a local variable outside unless it is mapped to an external variable using return. By using the global keyword, the converted variable's value can be used outside the function.
cs1qa
null
null
null
null
null
Question: What is the function of the global keyword in Task 1? Code: balance = 0 def deposit(money): # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance print('You deposited %d won' % money) global balance balance=balance+money # Do something on here ! def withdrawal(money): # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance print("You've withdrawn %d won" % money) if balance < money: print('But you only have %d won' % balance) return else: balance=balance-money # Do something on here ! def bank(): # Input : (None) No Input # Output : (None) No Output while True: global balance process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': return elif process=='d': money=input('How much do you want to deposit?') deposit(int(money)) elif process=='w': money=input('How much do you want to withdraw?') withdrawal(int(money)) elif process=='c': print('Your current balance is %d won' % balance) else : print('Please press d or w or c or return') bank()
8
2
TA
And %4.1f Please explain what happens when you print it using this
import elice_utils f = open("tpmon.txt","r") year = 1722 first_line = f.readline() while True: s = f.readline() if not s: break s.strip() l = s.split() flist = list(map(float, l)) wint_avg = (flist[0]+flist[1])/2 summ_avg = (flist[6]+flist[7])/2 year +=1 print('%d: %6.1f / %4.1f' % (year, wint_avg, summ_avg)) avg = sum(flist)/12 f2 = open("tpmon.csv","a") data = "%d,%.1f\n" % (year,avg) f2.write(data) f2.close() elice_utils.send_file('tpmon.csv')
16
16
variable
In %4.1f, the total number of digits is limited to 4 digits and the number of digits after the decimal point is limited to one digit, so if the number of digits exceeds this range, it will be rounded up and printed.
cs1qa
null
null
null
null
null
Question: And %4.1f Please explain what happens when you print it using this Code: import elice_utils f = open("tpmon.txt","r") year = 1722 first_line = f.readline() while True: s = f.readline() if not s: break s.strip() l = s.split() flist = list(map(float, l)) wint_avg = (flist[0]+flist[1])/2 summ_avg = (flist[6]+flist[7])/2 year +=1 print('%d: %6.1f / %4.1f' % (year, wint_avg, summ_avg)) avg = sum(flist)/12 f2 = open("tpmon.csv","a") data = "%d,%.1f\n" % (year,avg) f2.write(data) f2.close() elice_utils.send_file('tpmon.csv')
4
1
TA
Why should we apply float() after receiving input() in Task 2?
a =float(input('Side a: ')) print('Value of a is', a) b = float(input('Side b: ')) print('Value of b is', b) c = float(input('Side c: ')) print('Value of c is', c) def is_triangle(a, b, c): if a<b: if b<c: l=c m=b s=a else: l=b m=a s=c else: if a<c: l=c m=a s=c else: l=a m=b s=c if l<m+s: return True else: return False if is_triangle(a,b,c): print('YES') else: print('NO')
0
0
reasoning
Just input() recognizes it as a string, so you have to add float() to recognize it as a number!
cs1qa
null
null
null
null
null
Question: Why should we apply float() after receiving input() in Task 2? Code: a =float(input('Side a: ')) print('Value of a is', a) b = float(input('Side b: ')) print('Value of b is', b) c = float(input('Side c: ')) print('Value of c is', c) def is_triangle(a, b, c): if a<b: if b<c: l=c m=b s=a else: l=b m=a s=c else: if a<c: l=c m=a s=c else: l=a m=b s=c if l<m+s: return True else: return False if is_triangle(a,b,c): print('YES') else: print('NO')
5
1
TA
Are draw_animal() and show_animation() purposely created separately??
from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. paper = Canvas() paper.setWidth(500) paper.setHeight(200) Tank = Layer() tire1 = Circle(12, Point(-12, -5)) tire2 = Circle(12, Point(12, -5)) tire3 = Circle(12, Point(35, -5)) tire4 = Circle(12, Point(-35, -5)) body = Rectangle(70, 40, Point(0,-20)) head2 = Rectangle(50, 5, Point(50, -30)) tire1.setFillColor('black') tire2.setFillColor('black') tire3.setFillColor('black') tire4.setFillColor('black') body.setFillColor('green') head2.setFillColor('red') body.setDepth(60) Tank.add(tire1) Tank.add(tire2) Tank.add(tire3) Tank.add(tire4) Tank.add(body) Tank.add(head2) paper.add(Tank) Tank.moveTo(250, 100) def show_animation(): # Implement this function. paper = Canvas() paper.setWidth(500) paper.setHeight(200) Tank = Layer() tire1 = Circle(12, Point(-12, -5)) tire2 = Circle(12, Point(12, -5)) tire3 = Circle(12, Point(35, -5)) tire4 = Circle(12, Point(-35, -5)) body = Rectangle(70, 40, Point(0,-20)) head2 = Rectangle(50, 5, Point(50, -30)) tire1.setFillColor('black') tire2.setFillColor('black') tire3.setFillColor('black') tire4.setFillColor('black') body.setFillColor('green') head2.setFillColor('red') body.setDepth(60) Tank.add(tire1) Tank.add(tire2) Tank.add(tire3) Tank.add(tire4) Tank.add(body) Tank.add(head2) paper.add(Tank) Tank.moveTo(0, 100) delay = 0.2 for i in range(50): Tank.move(8,0) head2.move(0,10) sleep(delay) head2.move(0, -10) sleep(delay) draw_animal() show_animation()
null
null
reasoning
Oh, it wasn't on purpose, but I think it happened because I tried to print it out with the animation
cs1qa
null
null
null
null
null
Question: Are draw_animal() and show_animation() purposely created separately?? Code: from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. paper = Canvas() paper.setWidth(500) paper.setHeight(200) Tank = Layer() tire1 = Circle(12, Point(-12, -5)) tire2 = Circle(12, Point(12, -5)) tire3 = Circle(12, Point(35, -5)) tire4 = Circle(12, Point(-35, -5)) body = Rectangle(70, 40, Point(0,-20)) head2 = Rectangle(50, 5, Point(50, -30)) tire1.setFillColor('black') tire2.setFillColor('black') tire3.setFillColor('black') tire4.setFillColor('black') body.setFillColor('green') head2.setFillColor('red') body.setDepth(60) Tank.add(tire1) Tank.add(tire2) Tank.add(tire3) Tank.add(tire4) Tank.add(body) Tank.add(head2) paper.add(Tank) Tank.moveTo(250, 100) def show_animation(): # Implement this function. paper = Canvas() paper.setWidth(500) paper.setHeight(200) Tank = Layer() tire1 = Circle(12, Point(-12, -5)) tire2 = Circle(12, Point(12, -5)) tire3 = Circle(12, Point(35, -5)) tire4 = Circle(12, Point(-35, -5)) body = Rectangle(70, 40, Point(0,-20)) head2 = Rectangle(50, 5, Point(50, -30)) tire1.setFillColor('black') tire2.setFillColor('black') tire3.setFillColor('black') tire4.setFillColor('black') body.setFillColor('green') head2.setFillColor('red') body.setDepth(60) Tank.add(tire1) Tank.add(tire2) Tank.add(tire3) Tank.add(tire4) Tank.add(body) Tank.add(head2) paper.add(Tank) Tank.moveTo(0, 100) delay = 0.2 for i in range(50): Tank.move(8,0) head2.move(0,10) sleep(delay) head2.move(0, -10) sleep(delay) draw_animal() show_animation()
6
0
TA
What is the role of f_temp in task1?
def fibonacci(upper_bound): f1=0 f2=1 fibonacci=[f1,f2] f_temp=0 while 1: f_temp=f1 f_temp+=f2 if f_temp>upper_bound: break else: fibonacci.append(f_temp) f1=f2 f2=f_temp return fibonacci print(fibonacci(1000))
1
14
variable
After finding f_n, you need both f_n and f_(n-1) to find f_(n+1). At this time, f_n is stored for a while!
cs1qa
null
null
null
null
null
Question: What is the role of f_temp in task1? Code: def fibonacci(upper_bound): f1=0 f2=1 fibonacci=[f1,f2] f_temp=0 while 1: f_temp=f1 f_temp+=f2 if f_temp>upper_bound: break else: fibonacci.append(f_temp) f1=f2 f2=f_temp return fibonacci print(fibonacci(1000))
6
0
TA
Please explain what upper_bound does in Task 1
def fibonacci(upper_bound): a=0 b=1 c=0 result=[a,b] while a+b<=upper_bound: c=a+b result.append(c) a=b b=c return result print(fibonacci(1000))
0
10
variable
In task 1, upper_bound is used as a role to print a list of Fibonacci sequences until the value of a+b is less than upper_bound.
cs1qa
null
null
null
null
null
Question: Please explain what upper_bound does in Task 1 Code: def fibonacci(upper_bound): a=0 b=1 c=0 result=[a,b] while a+b<=upper_bound: c=a+b result.append(c) a=b b=c return result print(fibonacci(1000))
6
0
TA
What is the sum of
def fibonacci(upper_bound): a = [0, 1] i=0 while (a[i+1]+ a[i]) < upper_bound: x = a[i+1]+ a[i] a.append(x) i= i+1 return a print(fibonacci(1000))
0
7
variable
Two consecutive numbers in the list
cs1qa
null
null
null
null
null
Question: What is the sum of Code: def fibonacci(upper_bound): a = [0, 1] i=0 while (a[i+1]+ a[i]) < upper_bound: x = a[i+1]+ a[i] a.append(x) i= i+1 return a print(fibonacci(1000))
5
0
TA
Task 1: Explain what each of the global variables a, b, c is!
from cs1robots import * #load_world('worlds/add1.wld') #load_world('worlds/add2.wld') load_world('worlds/add34.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() a=1 b=0 c=0 def read(): global a,b,c while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() c=c+1 b=b+c*a c=0 a=a*10 hubo.move() a=1 def write(x): while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): for i in range(x%10): hubo.drop_beeper() x=int(x/10) hubo.move() read() turn_right() hubo.move() turn_right() read() hubo.turn_left() hubo.move() hubo.turn_left() write(b)
11
27
variable
a is the exponent of 10 (1 digit 1, 10 digit 10), c is each digit, b is the total
cs1qa
null
null
null
null
null
Question: Task 1: Explain what each of the global variables a, b, c is! Code: from cs1robots import * #load_world('worlds/add1.wld') #load_world('worlds/add2.wld') load_world('worlds/add34.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() a=1 b=0 c=0 def read(): global a,b,c while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() c=c+1 b=b+c*a c=0 a=a*10 hubo.move() a=1 def write(x): while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): for i in range(x%10): hubo.drop_beeper() x=int(x/10) hubo.move() read() turn_right() hubo.move() turn_right() read() hubo.turn_left() hubo.move() hubo.turn_left() write(b)
8
0
TA
Please explain the ‘r’ and ‘w’ variables of open().
from time import sleep import elice_utils def merge(input_filenames, output_filename): d = open(output_filename , 'w') a = len(input_filenames) for i in range(a): c = input_filenames[i] f = open(c, 'r') b = f.readlines() for x in range(len(b)): d.write(b[x]) f.close() d.close() pass merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
4
4
variable
r is used to read files and w is used to write files.
cs1qa
null
null
null
null
null
Question: Please explain the ‘r’ and ‘w’ variables of open(). Code: from time import sleep import elice_utils def merge(input_filenames, output_filename): d = open(output_filename , 'w') a = len(input_filenames) for i in range(a): c = input_filenames[i] f = open(c, 'r') b = f.readlines() for x in range(len(b)): d.write(b[x]) f.close() d.close() pass merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
8
0
TA
Please briefly explain what the meaning of "r" and "w" used to open!
from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here # ... file1=open(output_filename,"w") for file_names in input_filenames: file2=open(file_names,"r") for line in file2: file1.write(line) file2.close() file1.close() pass merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
6
8
variable
'r' means open for reading (default),'w' means open for writing, truncating the file first.
cs1qa
null
null
null
null
null
Question: Please briefly explain what the meaning of "r" and "w" used to open! Code: from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here # ... file1=open(output_filename,"w") for file_names in input_filenames: file2=open(file_names,"r") for line in file2: file1.write(line) file2.close() file1.close() pass merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
5
1
TA
Is that because I don't think I can access the ship and balls in show_animation??
from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. sun = Circle(18, Point(430, 220)) sun.setFillColor('yellow') sun.setDepth(30) paper.add(sun) flare = Circle(24, Point(430, 220)) flare.setFillColor('red') flare.setDepth(40) paper.add(flare) body = Rectangle(28, 10, Point(430, 110)) body.setFillColor('skyBlue') ship.add(body) ball1 = Circle(8, Point(430, 130)) ball2 = Circle(8, Point(430, 90)) ball1.setFillColor('yellow') ball2.setFillColor('yellow') balls.add(ball1) balls.add(ball2) def show_animation(): # Implement this function. delay = 1 for i in range(15): ship.move(-10, 0) balls.move(0,10) sleep(delay) ship.move(-10, 0) balls.move(0,-10) sleep(delay) paper = Canvas() paper.setBackgroundColor('white') paper.setWidth(500) paper.setHeight(300) paper.setTitle('Space') ship = Layer() paper.add(ship) ship.setDepth(10) balls = Layer() ship.add(balls) draw_animal() show_animation()
51
56
reasoning
Yes, yes, it seemed to be accessible only if the declaration was made globally outside.
cs1qa
null
null
null
null
null
Question: Is that because I don't think I can access the ship and balls in show_animation?? Code: from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. sun = Circle(18, Point(430, 220)) sun.setFillColor('yellow') sun.setDepth(30) paper.add(sun) flare = Circle(24, Point(430, 220)) flare.setFillColor('red') flare.setDepth(40) paper.add(flare) body = Rectangle(28, 10, Point(430, 110)) body.setFillColor('skyBlue') ship.add(body) ball1 = Circle(8, Point(430, 130)) ball2 = Circle(8, Point(430, 90)) ball1.setFillColor('yellow') ball2.setFillColor('yellow') balls.add(ball1) balls.add(ball2) def show_animation(): # Implement this function. delay = 1 for i in range(15): ship.move(-10, 0) balls.move(0,10) sleep(delay) ship.move(-10, 0) balls.move(0,-10) sleep(delay) paper = Canvas() paper.setBackgroundColor('white') paper.setWidth(500) paper.setHeight(300) paper.setTitle('Space') ship = Layer() paper.add(ship) ship.setDepth(10) balls = Layer() ship.add(balls) draw_animal() show_animation()
1
4
TA
Why did you make an exception after making two repetitions?
from cs1robots import * load_world("worlds/harvest2.wld") hubo=Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def harvest_up(): for i in range(5): hubo.pick_beeper() turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.turn_left() hubo.pick_beeper() def turn1(): turn_right() hubo.move() turn_right() hubo.move() def turn2(): for i in range(2): hubo.move() hubo.turn_left() hubo.turn_left() for i in range(6): hubo.move() for i in range(2): harvest_up() turn1() harvest_down() turn2() harvest_up() turn1() harvest_down()
38
45
reasoning
Since the process of changing the direction was specified as a function, an unnecessary change of direction occurred at the end when it was repeated 3 times, so after repeating 2 times to subtract the part, an exception without trun2 was made separately.
cs1qa
null
null
null
null
null
Question: Why did you make an exception after making two repetitions? Code: from cs1robots import * load_world("worlds/harvest2.wld") hubo=Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def harvest_up(): for i in range(5): hubo.pick_beeper() turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.turn_left() hubo.pick_beeper() def turn1(): turn_right() hubo.move() turn_right() hubo.move() def turn2(): for i in range(2): hubo.move() hubo.turn_left() hubo.turn_left() for i in range(6): hubo.move() for i in range(2): harvest_up() turn1() harvest_down() turn2() harvest_up() turn1() harvest_down()
4
0
TA
Please tell me the name of the function you are adding in Task1!
from cs1robots import * # load_world("worlds/add1.wld") # load_world("worlds/add2.wld") load_world("worlds/add34.wld") hubo=Robot() def turn_right(): for i in range (3): hubo.turn_left() hubo.turn_left() hubo.move() turn_right() def run1(): for i in range(9): hubo.move() def check1(): x=0 while hubo.on_beeper(): hubo.pick_beeper() x=x+1 turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop1(): (x1,x10)=check1() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() def check2(): x=0 turn_right() hubo.move() hubo.turn_left() hubo.turn_left() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop2(): (x1,x10)=check2() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() run1() drop1() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2()
20
58
variable
Whenever the beeper was picked up using the check function, the variable value was added by 1, and //10 was used to solve the case where the sum of the beepers exceeded 10.
cs1qa
null
null
null
null
null
Question: Please tell me the name of the function you are adding in Task1! Code: from cs1robots import * # load_world("worlds/add1.wld") # load_world("worlds/add2.wld") load_world("worlds/add34.wld") hubo=Robot() def turn_right(): for i in range (3): hubo.turn_left() hubo.turn_left() hubo.move() turn_right() def run1(): for i in range(9): hubo.move() def check1(): x=0 while hubo.on_beeper(): hubo.pick_beeper() x=x+1 turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop1(): (x1,x10)=check1() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() def check2(): x=0 turn_right() hubo.move() hubo.turn_left() hubo.turn_left() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop2(): (x1,x10)=check2() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() run1() drop1() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2()
3
3
TA
How did you determine if it was a window? Is there a reason you're so salty?
from cs1robots import * # Your code must work for both of the worlds below. # load_world('worlds/rain1.wld') load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. tim= Robot(beepers=100, avenue=2, street=6, orientation='E') # Now close all the windows in the house! tim.set_trace('blue') def turn_right(): for i in range(3): tim.turn_left() def turn_around(): for i in range(2): tim.turn_left() def walk(): if tim.front_is_clear(): tim.move() if not tim.on_beeper(): if tim.left_is_clear(): tim.move() if tim.left_is_clear(): turn_around() tim.move() turn_right() tim.move() else: turn_around() tim.move() turn_around() tim.drop_beeper() tim.move() else: turn_right() tim.move() tim.drop_beeper() tim.turn_left() tim.move() while not tim.on_beeper(): walk() tim.pick_beeper() tim.turn_left()
18
43
reasoning
I went one step forward and checked if there was a wall. If we just check that the left side is empty, we have confirmed that a problem occurs in the corner entering the inside, and to prevent this, we checked again.
cs1qa
null
null
null
null
null
Question: How did you determine if it was a window? Is there a reason you're so salty? Code: from cs1robots import * # Your code must work for both of the worlds below. # load_world('worlds/rain1.wld') load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. tim= Robot(beepers=100, avenue=2, street=6, orientation='E') # Now close all the windows in the house! tim.set_trace('blue') def turn_right(): for i in range(3): tim.turn_left() def turn_around(): for i in range(2): tim.turn_left() def walk(): if tim.front_is_clear(): tim.move() if not tim.on_beeper(): if tim.left_is_clear(): tim.move() if tim.left_is_clear(): turn_around() tim.move() turn_right() tim.move() else: turn_around() tim.move() turn_around() tim.drop_beeper() tim.move() else: turn_right() tim.move() tim.drop_beeper() tim.turn_left() tim.move() while not tim.on_beeper(): walk() tim.pick_beeper() tim.turn_left()
4
0
TA
Why did you use the while statement??
def fibonacci(upper_bound): pass result = [0, 1] while True: result.append(result[-1]+result[-2]) if result[-1] >= upper_bound: result.remove(result[-1]) return(result) print(fibonacci(1000))
2
7
reasoning
It is to repeat until the upper_bound is exceeded, and when the condition is satisfied, it is finished with return.
cs1qa
null
null
null
null
null
Question: Why did you use the while statement?? Code: def fibonacci(upper_bound): pass result = [0, 1] while True: result.append(result[-1]+result[-2]) if result[-1] >= upper_bound: result.remove(result[-1]) return(result) print(fibonacci(1000))
6
2
TA
What is the case for num1 and num2 that return False when is_valid?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 in correct_list or num2 in correct_list or num1==num2 or num1<0 or num1>23 or num2<0 or num2>23: return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1] != cards[num2][1]: correct_list.remove(num1) correct_list.remove(num2) print_cards() return False return True initialize() correct_list = list(range(24)) print_cards() correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while correct_list != list(range(24)): # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries += 1 ###########################################################################
58
68
variable
If the number of cards already matched, if the two numbers are the same, if it exceeds the range of 0 to 23
cs1qa
null
null
null
null
null
Question: What is the case for num1 and num2 that return False when is_valid? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 in correct_list or num2 in correct_list or num1==num2 or num1<0 or num1>23 or num2<0 or num2>23: return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1] != cards[num2][1]: correct_list.remove(num1) correct_list.remove(num2) print_cards() return False return True initialize() correct_list = list(range(24)) print_cards() correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while correct_list != list(range(24)): # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries += 1 ###########################################################################
3
4
TA
Why do you need to nest two for statements when implementing Task 5?
from cs1media import * # This code converts an image into a black & white poster. yellow = (255, 255, 0) blue = (0, 0, 255) green = (0, 255, 0) image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness > 200: image.set(x, y, yellow) elif average_brightness > 100: image.set(x, y, green) else: image.set(x, y, blue) image.show()
12
13
reasoning
This is to consider all points by multiplying the two.
cs1qa
null
null
null
null
null
Question: Why do you need to nest two for statements when implementing Task 5? Code: from cs1media import * # This code converts an image into a black & white poster. yellow = (255, 255, 0) blue = (0, 0, 255) green = (0, 255, 0) image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness > 200: image.set(x, y, yellow) elif average_brightness > 100: image.set(x, y, green) else: image.set(x, y, blue) image.show()
1
0
TA
Could you please explain what the run() function does?
from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def run(): hubo.turn_left() for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move() hubo.turn_left() hubo.move() for i in range(4): run() hubo.turn_left() for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move()
10
20
variable
It is a function to turn left once, go to the end, turn right again, move one space, rotate right again, move to the end, and finally look to the right.
cs1qa
null
null
null
null
null
Question: Could you please explain what the run() function does? Code: from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def run(): hubo.turn_left() for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move() hubo.turn_left() hubo.move() for i in range(4): run() hubo.turn_left() for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move()
6
1
TA
Why did you put lines 26~27?
from random import randint def drawing_integers(lb, ub, trials): a = [] for i in range(trials): b = randint(lb, ub) a.append(b) return a def average_integers(num_list): a = 0 for i in num_list: a += i return a/len(num_list) def count_integers(num_list): a = [] for i in num_list: k = num_list.count(i) for j in range(k): num_list.remove(i) a.append((i,k)) a.sort() return a list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
23
26
reasoning
When k number of same numbers are included, counting them as duplicates is (5,2), (5,1)
cs1qa
null
null
null
null
null
Question: Why did you put lines 26~27? Code: from random import randint def drawing_integers(lb, ub, trials): a = [] for i in range(trials): b = randint(lb, ub) a.append(b) return a def average_integers(num_list): a = 0 for i in num_list: a += i return a/len(num_list) def count_integers(num_list): a = [] for i in num_list: k = num_list.count(i) for j in range(k): num_list.remove(i) a.append((i,k)) a.sort() return a list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
4
3
TA
Task 4: Why do you need +1 and *40 in line 8?
import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = int((sin(x)+1)*40) # Change this line to print out sine curve correctly. output_str = '#' * character_count_per_line print (output_str)
7
7
reasoning
Task 4: Since we used a method of expressing # by plotting positive integer values, it was necessary to change the expressed values so that they are all positive and meaningful integers.
cs1qa
null
null
null
null
null
Question: Task 4: Why do you need +1 and *40 in line 8? Code: import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = int((sin(x)+1)*40) # Change this line to print out sine curve correctly. output_str = '#' * character_count_per_line print (output_str)
3
0
TA
What does the code in like 11-12 do?
from cs1robots import * create_world() hubo=Robot(orientation='S', avenue=7, street=4) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() while hubo.facing_north()==False: hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
10
11
code_explain
It makes the robot face toward North.
cs1qa
null
null
null
null
null
Question: What does the code in like 11-12 do? Code: from cs1robots import * create_world() hubo=Robot(orientation='S', avenue=7, street=4) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() while hubo.facing_north()==False: hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
4
4
TA
At tasks 4 and 5 How did you handle when the sine function value was negative
import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = int(40+40*sin(x)) # Change this line to print out sine curve correctly. output_str = ' ' * (character_count_per_line-1) + '*' print (output_str)
6
7
code_explain
Based on 40 character_count_per_line is Always be positive Was
cs1qa
null
null
null
null
null
Question: At tasks 4 and 5 How did you handle when the sine function value was negative Code: import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = int(40+40*sin(x)) # Change this line to print out sine curve correctly. output_str = ' ' * (character_count_per_line-1) + '*' print (output_str)
6
2
TA
What does initialize do?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] t=0 def initialize(): # initialize cards global cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list l=[] for i in range(24): l.append(cards.pop(random.randint(0,23-i))) cards=l ################################################################ count=0 def print_cards(): global t global count canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if correct_list.count(i)==1 or count==0: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) count=1 def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if correct_list.count(num1)==0 and correct_list.count(num2)==0: if num1!=num2: if num1<=23 and num1>=0 and num2<=23 and num2>=0: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### global cards global correct_list A=cards[num1] B=cards[num2] correct_list.append(num1) correct_list.append(num2) print_cards() correct_list.remove(num1) correct_list.remove(num2) if A[1]==B[1]: correct_list.append(num1) correct_list.append(num2) return True print_cards() return False initialize() print_cards() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)<23: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if tries>3: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") elif tries==1: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif tries==2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### tries=tries+1# 3-2-5. Update number of tries (global variable, tries) ###########################################################################
15
38
variable
Set the size and picture of the card, add it to the canvas and randomly shuffle the order of the cards before expressing it with numbers or painting pads. Then enter the name and picture of the card in the card list.
cs1qa
null
null
null
null
null
Question: What does initialize do? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] t=0 def initialize(): # initialize cards global cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list l=[] for i in range(24): l.append(cards.pop(random.randint(0,23-i))) cards=l ################################################################ count=0 def print_cards(): global t global count canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if correct_list.count(i)==1 or count==0: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) count=1 def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if correct_list.count(num1)==0 and correct_list.count(num2)==0: if num1!=num2: if num1<=23 and num1>=0 and num2<=23 and num2>=0: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### global cards global correct_list A=cards[num1] B=cards[num2] correct_list.append(num1) correct_list.append(num2) print_cards() correct_list.remove(num1) correct_list.remove(num2) if A[1]==B[1]: correct_list.append(num1) correct_list.append(num2) return True print_cards() return False initialize() print_cards() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)<23: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if tries>3: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") elif tries==1: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif tries==2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### tries=tries+1# 3-2-5. Update number of tries (global variable, tries) ###########################################################################
3
2
TA
Please also briefly explain the reason why you should use the while statement, not the for statement loop, in the process of picking up the beeper.
from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash4.wld') # load_world('worlds/trash4.wld') hubo = Robot() hubo.set_trace('blue') def turn_right () : for i in range (3) : hubo.turn_left() hubo.turn_left() while True : while hubo.front_is_clear () : if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() hubo.move() if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() turn_right () if hubo.front_is_clear() : hubo.move () else : break turn_right () while hubo.front_is_clear () : if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() hubo.move () if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() hubo.turn_left () if hubo.front_is_clear () : hubo.move () else : break hubo.turn_left() while not hubo.facing_north () : hubo.turn_left() while hubo.front_is_clear () : hubo.move() hubo.turn_left() while hubo.front_is_clear () : hubo.move() hubo.turn_left () while hubo.front_is_clear () : hubo.move() while hubo.carries_beepers () : hubo.drop_beeper ()
14
34
reasoning
This is because the number of beepers is different for each location where each beeper exists, so the while statement was used because it was not possible to pick up the beeper only a specific number of times using for.
cs1qa
null
null
null
null
null
Question: Please also briefly explain the reason why you should use the while statement, not the for statement loop, in the process of picking up the beeper. Code: from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash4.wld') # load_world('worlds/trash4.wld') hubo = Robot() hubo.set_trace('blue') def turn_right () : for i in range (3) : hubo.turn_left() hubo.turn_left() while True : while hubo.front_is_clear () : if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() hubo.move() if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() turn_right () if hubo.front_is_clear() : hubo.move () else : break turn_right () while hubo.front_is_clear () : if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() hubo.move () if hubo.on_beeper () : while hubo.on_beeper () : hubo.pick_beeper() hubo.turn_left () if hubo.front_is_clear () : hubo.move () else : break hubo.turn_left() while not hubo.facing_north () : hubo.turn_left() while hubo.front_is_clear () : hubo.move() hubo.turn_left() while hubo.front_is_clear () : hubo.move() hubo.turn_left () while hubo.front_is_clear () : hubo.move() while hubo.carries_beepers () : hubo.drop_beeper ()
8
2
TA
What method did you use to extract the desired data (number) from Task 3?
import elice_utils import time #1 f=open("tpmon.txt", "r") year=1723 for line in f: a=line.strip().split() if a[0]=='MONTHLY': continue winter_avg=(float(a[0])+float(a[1]))/2 summer_avg=(float(a[6])+float(a[7]))/2 print('%d: %6.1f / %4.1f' % (year, winter_avg, summer_avg)) year+=1 f.close() #2 f=open("tpmon.txt", "r") year=1723 f1=open("tpmon.csv", 'w') for line in f: a=line.strip().split() b=0 if a[0]=='MONTHLY': continue for i in range(12): b+=float(a[i]) b/=12 f1.write(str(year)) f1.write(", ") f1.write(str(b)) f1.write("\n") year+=1 f1.close() time.sleep(0.5) f1=open("tpmon.csv", 'r') #for line in f1: #print(line.strip()) #f1.close() #f.close() elice_utils.send_file('tpmon.csv')
5
30
code_explain
After reading from the file line by line through the for statement, each number was divided through line.strip().split() in each process, and the numbers were retrieved one by one using the for statement again!!
cs1qa
null
null
null
null
null
Question: What method did you use to extract the desired data (number) from Task 3? Code: import elice_utils import time #1 f=open("tpmon.txt", "r") year=1723 for line in f: a=line.strip().split() if a[0]=='MONTHLY': continue winter_avg=(float(a[0])+float(a[1]))/2 summer_avg=(float(a[6])+float(a[7]))/2 print('%d: %6.1f / %4.1f' % (year, winter_avg, summer_avg)) year+=1 f.close() #2 f=open("tpmon.txt", "r") year=1723 f1=open("tpmon.csv", 'w') for line in f: a=line.strip().split() b=0 if a[0]=='MONTHLY': continue for i in range(12): b+=float(a[i]) b/=12 f1.write(str(year)) f1.write(", ") f1.write(str(b)) f1.write("\n") year+=1 f1.close() time.sleep(0.5) f1=open("tpmon.csv", 'r') #for line in f1: #print(line.strip()) #f1.close() #f.close() elice_utils.send_file('tpmon.csv')
2
0
TA
Please explain task1 go function
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def go(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() for i in range(6): go() hubo.turn_left() def right(): go() turn_right() for i in range(5): go() hubo.turn_left() def left(): go() hubo.turn_left() for i in range(5): go() turn_right() left() right() left() right() left() hubo.turn_left()
9
12
variable
In task1, the go function is a function to proceed forward and if there is a coin, give it together.
cs1qa
null
null
null
null
null
Question: Please explain task1 go function Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def go(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() for i in range(6): go() hubo.turn_left() def right(): go() turn_right() for i in range(5): go() hubo.turn_left() def left(): go() hubo.turn_left() for i in range(5): go() turn_right() left() right() left() right() left() hubo.turn_left()
1
2
TA
At number 3 hubo.move() time.sleep(0.2) hubo.drop_beeper() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) Can you explain what this part does?
from cs1robots import * load_world('worlds/newspaper.wld') import time hubo=Robot(beepers=1) hubo.set_trace('blue') def turn_right(): for i in range (3): hubo.turn_left() time.sleep(0.2) def go_up_stair(): hubo.turn_left() time.sleep(0.2) hubo.move() time.sleep(0.2) turn_right() hubo.move() time.sleep(0.2) def go_down_stair(): hubo.move() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) hubo.move() time.sleep(0.2) turn_right() for i in range(4): hubo.move() time.sleep(0.2) go_up_stair() hubo.move() time.sleep(0.2) hubo.drop_beeper() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) for i in range(4): hubo.move() time.sleep(0.2) go_down_stair() hubo.move()
35
42
code_explain
This is the process of the robot turning around after delivering the newspaper. Since it is used only once, no function is specified.
cs1qa
null
null
null
null
null
Question: At number 3 hubo.move() time.sleep(0.2) hubo.drop_beeper() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) Can you explain what this part does? Code: from cs1robots import * load_world('worlds/newspaper.wld') import time hubo=Robot(beepers=1) hubo.set_trace('blue') def turn_right(): for i in range (3): hubo.turn_left() time.sleep(0.2) def go_up_stair(): hubo.turn_left() time.sleep(0.2) hubo.move() time.sleep(0.2) turn_right() hubo.move() time.sleep(0.2) def go_down_stair(): hubo.move() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) hubo.move() time.sleep(0.2) turn_right() for i in range(4): hubo.move() time.sleep(0.2) go_up_stair() hubo.move() time.sleep(0.2) hubo.drop_beeper() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) hubo.turn_left() time.sleep(0.2) for i in range(4): hubo.move() time.sleep(0.2) go_down_stair() hubo.move()
2
4
TA
After completing task 5, please explain!
from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... hubo=Robot() hubo.set_trace('blue') # ...
null
null
code_explain
As a natural proposition, I wrote the code to loop back and forth indefinitely, and whenever there is no wall in front of it, if there is a wall in front of it when I need to break it, the loop escapes.
cs1qa
null
null
null
null
null
Question: After completing task 5, please explain! Code: from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... hubo=Robot() hubo.set_trace('blue') # ...
6
1
TA
Why did you use set() in count_integers() in task2?
from random import * def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ arr = [] for i in range(trials): x = randint(lb,ub) arr.append(x) return arr pass def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ s = 0 for i in range(len(num_list)): s += num_list[i] return s/len(num_list) pass def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ list1 = list(set(num_list)) list2 = [] for i in range(len(list1)): list2.append((list1[i], num_list.count(list1[i]))) return list2 pass # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
37
37
reasoning
After removing the duplicated elements in advance, I saved a tuple of elements and number of elements in list2.
cs1qa
null
null
null
null
null
Question: Why did you use set() in count_integers() in task2? Code: from random import * def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ arr = [] for i in range(trials): x = randint(lb,ub) arr.append(x) return arr pass def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ s = 0 for i in range(len(num_list)): s += num_list[i] return s/len(num_list) pass def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ list1 = list(set(num_list)) list2 = [] for i in range(len(list1)): list2.append((list1[i], num_list.count(list1[i]))) return list2 pass # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
6
1
TA
Is there any reason you used for _ in line 14?
import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ lst = [] for _ in range(trials): lst.append(random.randint(lb,ub)) return lst def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ return sum(num_list) / len(num_list) def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ lst = [] s = min(num_list) e = max(num_list) for i in range(s,e+1): lst.append((i,num_list.count(i))) return lst # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
13
14
reasoning
The variable used in the for-loop is not used anywhere, so I wrote it with a name that is not used well/
cs1qa
null
null
null
null
null
Question: Is there any reason you used for _ in line 14? Code: import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ lst = [] for _ in range(trials): lst.append(random.randint(lb,ub)) return lst def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ return sum(num_list) / len(num_list) def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ lst = [] s = min(num_list) e = max(num_list) for i in range(s,e+1): lst.append((i,num_list.count(i))) return lst # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
6
2
TA
check function Please explain
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 in correct_list or num2 in correct_list: return False elif num1 == num2: return False elif not 0 <= num1 <=23 or not 0 <= num2 <=23: return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) if cards[num1][1] == cards[num2][1]: print_cards() return True else: print_cards() correct_list.pop() correct_list.pop() print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") for i in range(24): correct_list.append(i) print_cards() for i in range(24): correct_list.remove(i) print_cards() ############################################################################### while len(correct_list) <=23: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
81
101
variable
After adding the two drawn cards to the correct list, if they are the same, print true and send out true, and if the two cards are different, remove them from the correct list and send false
cs1qa
null
null
null
null
null
Question: check function Please explain Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 in correct_list or num2 in correct_list: return False elif num1 == num2: return False elif not 0 <= num1 <=23 or not 0 <= num2 <=23: return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) if cards[num1][1] == cards[num2][1]: print_cards() return True else: print_cards() correct_list.pop() correct_list.pop() print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") for i in range(24): correct_list.append(i) print_cards() for i in range(24): correct_list.remove(i) print_cards() ############################################################################### while len(correct_list) <=23: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
10
0
student
_scene.add(self.layer) There is something like this in the description, so what should I do?
from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ _scene=Canvas(600,200,'lightblue') OLEV=Rectangle(150,10,Point(325,115)) OLEV.setFillColor('white') _scene.add(OLEV) """ define your own objects, e.g. Mario and Mushroom class Mushroom (object): def __init__(self, x, y): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) mushroom.add(lowermush) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom # save mushroom shape in the class _scene.add(self.layer) # add to global Canvas class Mario (object): def __init__(self, ... self.layer = Layer() ... _scene.add(self.layer) """ SIZE=['Normal','Big'] class Car (object): def __init__(self, size): assert size in SIZE self.size=size car = Layer() if size=='Normal': body=Rectangle(150,50,Point(125,75)) t1=Circle(15,Point(90,100)) t11=Circle(10,Point(90,100)) t2=Circle(15,Point(160,100)) t22=Circle(10,Point(160,100)) body.setFillColor('lightgreen') t1.setFillColor('black') t11.setFillColor('white') t2.setFillColor('black') t22.setFillColor('white') l11=Path(Point(90,100),Point(99.51,103.09)) l12=Path(Point(90,100),Point(90,110)) l13=Path(Point(90,100),Point(80.49,103.09)) l14=Path(Point(90,100),Point(84.12,91.91)) l15=Path(Point(90,100),Point(95.88,91.91)) l21=Path(Point(160,100),Point(169.51,103.09)) l22=Path(Point(160,100),Point(160,110)) l23=Path(Point(160,100),Point(150.49,103.09)) l24=Path(Point(160,100),Point(154.12,91.91)) l25=Path(Point(160,100),Point(165.88,91.91)) car.add(body) car.add(t1) car.add(t11) car.add(t2) car.add(t22) car.add(l11) car.add(l12) car.add(l13) car.add(l14) car.add(l15) car.add(l21) car.add(l22) car.add(l23) car.add(l24) car.add(l25) elif size=='Big': body=Rectangle(275,75,Point(325,37.5)) t1=Circle(15,Point(90,100)) t11=Circle(10,Point(90,100)) t2=Circle(15,Point(160,100)) t22=Circle(10,Point(160,100)) body.setFillColor('lightgreen') t1.setFillColor('black') t11.setFillColor('white') t2.setFillColor('black') t22.setFillColor('white') l11=Path(Point(90,100),Point(99.51,103.09)) l12=Path(Point(90,100),Point(90,110)) l13=Path(Point(90,100),Point(80.49,103.09)) l14=Path(Point(90,100),Point(84.12,91.91)) l15=Path(Point(90,100),Point(95.88,91.91)) l21=Path(Point(160,100),Point(169.51,103.09)) l22=Path(Point(160,100),Point(160,110)) l23=Path(Point(160,100),Point(150.49,103.09)) l24=Path(Point(160,100),Point(154.12,91.91)) l25=Path(Point(160,100),Point(165.88,91.91)) car.add(body) car.add(t1) car.add(t11) car.add(t2) car.add(t22) car.add(l11) car.add(l12) car.add(l13) car.add(l14) car.add(l15) car.add(l21) car.add(l22) car.add(l23) car.add(l24) car.add(l25) self.layer=car _scene.add(self.layer) def move(self, x, y): self.layer.move(x,y) create_world() # define your objects, e.g. mario = Mario('blue', 'normal') car=Car('Normal') for i in range(20): sleep(0.1) car.move(10,0) car=Car('Big') for i in range(10): sleep(0.1) car.move(20,0) # write your animation scenario here
6
28
code_understanding
You are creating two canvases now
cs1qa
null
null
null
null
null
Question: _scene.add(self.layer) There is something like this in the description, so what should I do? Code: from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ _scene=Canvas(600,200,'lightblue') OLEV=Rectangle(150,10,Point(325,115)) OLEV.setFillColor('white') _scene.add(OLEV) """ define your own objects, e.g. Mario and Mushroom class Mushroom (object): def __init__(self, x, y): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) mushroom.add(lowermush) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom # save mushroom shape in the class _scene.add(self.layer) # add to global Canvas class Mario (object): def __init__(self, ... self.layer = Layer() ... _scene.add(self.layer) """ SIZE=['Normal','Big'] class Car (object): def __init__(self, size): assert size in SIZE self.size=size car = Layer() if size=='Normal': body=Rectangle(150,50,Point(125,75)) t1=Circle(15,Point(90,100)) t11=Circle(10,Point(90,100)) t2=Circle(15,Point(160,100)) t22=Circle(10,Point(160,100)) body.setFillColor('lightgreen') t1.setFillColor('black') t11.setFillColor('white') t2.setFillColor('black') t22.setFillColor('white') l11=Path(Point(90,100),Point(99.51,103.09)) l12=Path(Point(90,100),Point(90,110)) l13=Path(Point(90,100),Point(80.49,103.09)) l14=Path(Point(90,100),Point(84.12,91.91)) l15=Path(Point(90,100),Point(95.88,91.91)) l21=Path(Point(160,100),Point(169.51,103.09)) l22=Path(Point(160,100),Point(160,110)) l23=Path(Point(160,100),Point(150.49,103.09)) l24=Path(Point(160,100),Point(154.12,91.91)) l25=Path(Point(160,100),Point(165.88,91.91)) car.add(body) car.add(t1) car.add(t11) car.add(t2) car.add(t22) car.add(l11) car.add(l12) car.add(l13) car.add(l14) car.add(l15) car.add(l21) car.add(l22) car.add(l23) car.add(l24) car.add(l25) elif size=='Big': body=Rectangle(275,75,Point(325,37.5)) t1=Circle(15,Point(90,100)) t11=Circle(10,Point(90,100)) t2=Circle(15,Point(160,100)) t22=Circle(10,Point(160,100)) body.setFillColor('lightgreen') t1.setFillColor('black') t11.setFillColor('white') t2.setFillColor('black') t22.setFillColor('white') l11=Path(Point(90,100),Point(99.51,103.09)) l12=Path(Point(90,100),Point(90,110)) l13=Path(Point(90,100),Point(80.49,103.09)) l14=Path(Point(90,100),Point(84.12,91.91)) l15=Path(Point(90,100),Point(95.88,91.91)) l21=Path(Point(160,100),Point(169.51,103.09)) l22=Path(Point(160,100),Point(160,110)) l23=Path(Point(160,100),Point(150.49,103.09)) l24=Path(Point(160,100),Point(154.12,91.91)) l25=Path(Point(160,100),Point(165.88,91.91)) car.add(body) car.add(t1) car.add(t11) car.add(t2) car.add(t22) car.add(l11) car.add(l12) car.add(l13) car.add(l14) car.add(l15) car.add(l21) car.add(l22) car.add(l23) car.add(l24) car.add(l25) self.layer=car _scene.add(self.layer) def move(self, x, y): self.layer.move(x,y) create_world() # define your objects, e.g. mario = Mario('blue', 'normal') car=Car('Normal') for i in range(20): sleep(0.1) car.move(10,0) car=Car('Big') for i in range(10): sleep(0.1) car.move(20,0) # write your animation scenario here
2
4
TA
After completing task 5, please explain!
from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... hubo=Robot() hubo.set_trace('blue') # ...
null
null
code_explain
As a natural proposition, I wrote the code to loop back and forth indefinitely, and whenever there is no wall in front of it, if there is a wall in front of it when I need to break it, the loop escapes.
cs1qa
null
null
null
null
null
Question: After completing task 5, please explain! Code: from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... hubo=Robot() hubo.set_trace('blue') # ...
8
1
TA
What is f.readlines()?
f = open('average-latitude-longitude-countries.csv', 'r') code_name = [] code_lat_long = [] l = f.readlines() for n in range(len(l)-1): i = n+1 code = l[i].split(',')[0].replace('"','') if l[i].count(',')==3: name = l[i].split(',')[1].replace('"','') else: name = l[i].split(',')[1].replace('"','')+','+l[i].split(',')[2].replace('"','') code_name.append((code, name)) lat = float(l[i].split(',')[-2]) long = float(l[i].split(',')[-1]) code_lat_long.append((code, (lat, long))) if lat<0: print(name) print(code_name) print(code_lat_long) what_code = input('Enter country code: ') for i in range(len(code_name)): if what_code == code_name[i][0]: print(code_name[i][1]) break
3
3
variable
Create a list l where each line is a string element, similar to the method used in task2
cs1qa
null
null
null
null
null
Question: What is f.readlines()? Code: f = open('average-latitude-longitude-countries.csv', 'r') code_name = [] code_lat_long = [] l = f.readlines() for n in range(len(l)-1): i = n+1 code = l[i].split(',')[0].replace('"','') if l[i].count(',')==3: name = l[i].split(',')[1].replace('"','') else: name = l[i].split(',')[1].replace('"','')+','+l[i].split(',')[2].replace('"','') code_name.append((code, name)) lat = float(l[i].split(',')[-2]) long = float(l[i].split(',')[-1]) code_lat_long.append((code, (lat, long))) if lat<0: print(name) print(code_name) print(code_lat_long) what_code = input('Enter country code: ') for i in range(len(code_name)): if what_code == code_name[i][0]: print(code_name[i][1]) break
9
0
student
so is the attribute a boolean?
# Copy your "Memento" code from the task in Lab 6. from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = range(24) class Card: def __init__(self, n, i): self.name = n self.img = i def initialize(): # initialize cards for i in range(6): for k in range(4): ncard = Card(names[i], Image(path + names[i])) # ncard.img = Image(path + names[i]) # ncard.name = names[i] # img = Image(path + names[i]) # temp_tuple = (img, names[i]) cards.append(ncard) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].img.moveTo(i_w + w, i_h + h) canvas.add(cards[i].img) else: num_pads[i].moveTo(i_w + w, i_h + h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 != num2: if num2 not in correct_list: if num1 not in correct_list: if 0 <= num1 <= 23: if 0 <= num2 <= 23: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1].name == cards[num2].name: print_cards() return True correct_list.remove(num2) correct_list.remove(num1) print_cards() return False initialize() print_cards() time.sleep(3) correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list) // 2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): tries = tries + 1 continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ##################################################################
18
21
code_understanding
That differs by your coding can be a boolean
cs1qa
null
null
null
null
null
Question: so is the attribute a boolean? Code: # Copy your "Memento" code from the task in Lab 6. from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = range(24) class Card: def __init__(self, n, i): self.name = n self.img = i def initialize(): # initialize cards for i in range(6): for k in range(4): ncard = Card(names[i], Image(path + names[i])) # ncard.img = Image(path + names[i]) # ncard.name = names[i] # img = Image(path + names[i]) # temp_tuple = (img, names[i]) cards.append(ncard) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].img.moveTo(i_w + w, i_h + h) canvas.add(cards[i].img) else: num_pads[i].moveTo(i_w + w, i_h + h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 != num2: if num2 not in correct_list: if num1 not in correct_list: if 0 <= num1 <= 23: if 0 <= num2 <= 23: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1].name == cards[num2].name: print_cards() return True correct_list.remove(num2) correct_list.remove(num1) print_cards() return False initialize() print_cards() time.sleep(3) correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list) // 2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): tries = tries + 1 continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ##################################################################
2
1
TA
Please explain why you put the turn function in task2
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=20) hubo.set_trace('blue') def course(): for i in range(10): if hubo.front_is_clear(): if not hubo.on_beeper(): hubo.drop_beeper() hubo.move() else: if hubo.on_beeper(): hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() else: hubo.drop_beeper() hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() hubo.turn_left() hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def turn(): if not hubo.on_beeper(): hubo.drop_beeper() turn_right() hubo.move() turn_right() hubo.move() for i in range(2): course() turn() course() if not hubo.on_beeper(): hubo.drop_beeper()
34
39
reasoning
The reason for using the turn() function is Going in a straight line and then coming back as a unit There is no case of going up at the end, so I used a turn
cs1qa
null
null
null
null
null
Question: Please explain why you put the turn function in task2 Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=20) hubo.set_trace('blue') def course(): for i in range(10): if hubo.front_is_clear(): if not hubo.on_beeper(): hubo.drop_beeper() hubo.move() else: if hubo.on_beeper(): hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() else: hubo.drop_beeper() hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() hubo.turn_left() hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def turn(): if not hubo.on_beeper(): hubo.drop_beeper() turn_right() hubo.move() turn_right() hubo.move() for i in range(2): course() turn() course() if not hubo.on_beeper(): hubo.drop_beeper()
9
0
student
In Task 1, it was written even though I didn't enter .jpg. Why?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 class Card(object): def __init__(self, img, name, complete): self.img = img self.name = name self.complete = complete def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) cards.append(Card(img,names[i],'no')) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].img.moveTo(i_w + w, i_h+h) canvas.add(cards[i].img) num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) for i in range(len(num_pads)): canvas.remove(cards[i].img) def is_valid(num1, num2): if cards[num1].complete=='yes': return False pass if cards[num2].complete=='yes': return False pass if num1==num2: return False pass if num1>23 or num1<0: return False pass if num2>23 or num2<0: ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### return False pass return True def check(num1, num2): canvas.add(cards[num1].img) canvas.add(cards[num2].img) time.sleep(1) if cards[num1].name==cards[num2].name: # TA : cards[num1][0] is image, cards[num1][1] is name of image cards[num1].complete='yes' cards[num2].complete='yes' return True else: ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### canvas.remove(cards[num1].img) canvas.remove(cards[num2].img) return False def checkcomplete(): a = [] for i in range(len(cards)): a.append(cards[i].complete) return a initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") tries = 1 ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if checkcomplete().count('no')>0: print(str(tries) + "th try. You got " + str(checkcomplete().count('yes')//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### tries = tries + 1 else: break # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
8
9
code_understanding
Each element in the names list contains the string ".jpg".
cs1qa
null
null
null
null
null
Question: In Task 1, it was written even though I didn't enter .jpg. Why? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 class Card(object): def __init__(self, img, name, complete): self.img = img self.name = name self.complete = complete def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) cards.append(Card(img,names[i],'no')) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ random.shuffle(cards) # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].img.moveTo(i_w + w, i_h+h) canvas.add(cards[i].img) num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) for i in range(len(num_pads)): canvas.remove(cards[i].img) def is_valid(num1, num2): if cards[num1].complete=='yes': return False pass if cards[num2].complete=='yes': return False pass if num1==num2: return False pass if num1>23 or num1<0: return False pass if num2>23 or num2<0: ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### return False pass return True def check(num1, num2): canvas.add(cards[num1].img) canvas.add(cards[num2].img) time.sleep(1) if cards[num1].name==cards[num2].name: # TA : cards[num1][0] is image, cards[num1][1] is name of image cards[num1].complete='yes' cards[num2].complete='yes' return True else: ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### canvas.remove(cards[num1].img) canvas.remove(cards[num2].img) return False def checkcomplete(): a = [] for i in range(len(cards)): a.append(cards[i].complete) return a initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") tries = 1 ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if checkcomplete().count('no')>0: print(str(tries) + "th try. You got " + str(checkcomplete().count('yes')//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### tries = tries + 1 else: break # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
1
1
TA
Is there any reason why you put in sleep??
from cs1robots import* import time load_world("worlds/hurdles1.wld") hubo = Robot(beepers=1) hubo.set_trace("blue") speed = 0.3 def turn_right() : for i in range(3) : hubo.turn_left() def huddle() : hubo.turn_left() time.sleep(speed) hubo.move() time.sleep(speed) for i in range(2) : turn_right() time.sleep(speed) hubo.move() time.sleep(speed) hubo.turn_left() time.sleep(speed) hubo.move() time.sleep(speed) hubo.move() for i in range(4) : huddle() hubo.pick_beeper()
12
27
reasoning
Oh, the speed was too frustrating, so I just adjusted it as a variable at once
cs1qa
null
null
null
null
null
Question: Is there any reason why you put in sleep?? Code: from cs1robots import* import time load_world("worlds/hurdles1.wld") hubo = Robot(beepers=1) hubo.set_trace("blue") speed = 0.3 def turn_right() : for i in range(3) : hubo.turn_left() def huddle() : hubo.turn_left() time.sleep(speed) hubo.move() time.sleep(speed) for i in range(2) : turn_right() time.sleep(speed) hubo.move() time.sleep(speed) hubo.turn_left() time.sleep(speed) hubo.move() time.sleep(speed) hubo.move() for i in range(4) : huddle() hubo.pick_beeper()
1
0
TA
In task1, you set the variables a, b, and c differently in the for statement. Did you intentionally do it differently?
from cs1robots import* create_world() hubo=Robot() hubo.set_trace("blue") def turn_right(): for a in range(3): hubo.turn_left() def charge(): for b in range(9): hubo.move() hubo.turn_left() charge() turn_right() hubo.move() turn_right() charge() for c in range(4): hubo.turn_left() hubo.move() hubo.turn_left() charge() turn_right() hubo.move() turn_right() charge()
null
null
reasoning
Yes, I did it differently to differentiate
cs1qa
null
null
null
null
null
Question: In task1, you set the variables a, b, and c differently in the for statement. Did you intentionally do it differently? Code: from cs1robots import* create_world() hubo=Robot() hubo.set_trace("blue") def turn_right(): for a in range(3): hubo.turn_left() def charge(): for b in range(9): hubo.move() hubo.turn_left() charge() turn_right() hubo.move() turn_right() charge() for c in range(4): hubo.turn_left() hubo.move() hubo.turn_left() charge() turn_right() hubo.move() turn_right() charge()
3
2
TA
In task 3, tell us the role of the st function.
from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') my_robot=Robot() my_robot.set_trace('red') def st(): while(my_robot.front_is_clear()): my_robot.move() while(my_robot.on_beeper()): my_robot.pick_beeper() def turn_right(): for i in range(3): my_robot.turn_left() def zig(): st() my_robot.turn_left() my_robot.move() my_robot.turn_left() if my_robot.on_beeper(): my_robot.pick_beeper() st() turn_right() if not my_robot.front_is_clear(): return my_robot.move() turn_right() if my_robot.on_beeper(): my_robot.pick_beeper() for i in range(5): zig() for i in range(2): my_robot.turn_left() st() my_robot.turn_left()
9
13
variable
It is a function to go straight until an obstacle comes in front of you, but if there is a beeper, it is a function to pick up everything.
cs1qa
null
null
null
null
null
Question: In task 3, tell us the role of the st function. Code: from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') my_robot=Robot() my_robot.set_trace('red') def st(): while(my_robot.front_is_clear()): my_robot.move() while(my_robot.on_beeper()): my_robot.pick_beeper() def turn_right(): for i in range(3): my_robot.turn_left() def zig(): st() my_robot.turn_left() my_robot.move() my_robot.turn_left() if my_robot.on_beeper(): my_robot.pick_beeper() st() turn_right() if not my_robot.front_is_clear(): return my_robot.move() turn_right() if my_robot.on_beeper(): my_robot.pick_beeper() for i in range(5): zig() for i in range(2): my_robot.turn_left() st() my_robot.turn_left()
5
2
student
Does the pass given at first have a special meaning?
from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. # load_world('worlds/hurdles1.wld') load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') def jump_one_hurdle(): pass my_robot = Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() def go(): if my_robot.front_is_clear(): my_robot.move() else : my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() #while not my_robot.on_beeper(): # go() #my_robot.pick_beeper() jump_one_hurdle()
11
12
code_understanding
pass is a word that is inserted randomly when a function is defined, but the code is not written inside the function yet.
cs1qa
null
null
null
null
null
Question: Does the pass given at first have a special meaning? Code: from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. # load_world('worlds/hurdles1.wld') load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') def jump_one_hurdle(): pass my_robot = Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() def go(): if my_robot.front_is_clear(): my_robot.move() else : my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() #while not my_robot.on_beeper(): # go() #my_robot.pick_beeper() jump_one_hurdle()
3
3
TA
Please explain how you wrote the code for the protruding part in the middle of the rain2 world!
from cs1robots import * # Your code must work for both of the worlds below. # load_world('worlds/rain1.wld') load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace("blue") def turn_right(): for i in range(3): my_robot.turn_left() def change_direction(): if not my_robot.front_is_clear(): if my_robot.right_is_clear(): turn_right() elif my_robot.left_is_clear(): my_robot.turn_left() def move_or_close_window(): my_robot.move() if my_robot.right_is_clear and my_robot.left_is_clear(): my_robot.drop_beeper() my_robot.move() my_robot.turn_left() my_robot.move() my_robot.move() change_direction() while my_robot.front_is_clear(): move_or_close_window() change_direction() while my_robot.front_is_clear(): move_or_close_window() change_direction() move_or_close_window() move_or_close_window() if not my_robot.front_is_clear(): turn_right() my_robot.move() my_robot.turn_left() my_robot.move() while my_robot.front_is_clear(): move_or_close_window() change_direction() my_robot.move() my_robot.move() my_robot.turn_left() # Now close all the windows in the house!
20
23
code_explain
If there is a protruding part in the middle, the cord is squeezed so that only that part is properly passed.
cs1qa
null
null
null
null
null
Question: Please explain how you wrote the code for the protruding part in the middle of the rain2 world! Code: from cs1robots import * # Your code must work for both of the worlds below. # load_world('worlds/rain1.wld') load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace("blue") def turn_right(): for i in range(3): my_robot.turn_left() def change_direction(): if not my_robot.front_is_clear(): if my_robot.right_is_clear(): turn_right() elif my_robot.left_is_clear(): my_robot.turn_left() def move_or_close_window(): my_robot.move() if my_robot.right_is_clear and my_robot.left_is_clear(): my_robot.drop_beeper() my_robot.move() my_robot.turn_left() my_robot.move() my_robot.move() change_direction() while my_robot.front_is_clear(): move_or_close_window() change_direction() while my_robot.front_is_clear(): move_or_close_window() change_direction() move_or_close_window() move_or_close_window() if not my_robot.front_is_clear(): turn_right() my_robot.move() my_robot.turn_left() my_robot.move() while my_robot.front_is_clear(): move_or_close_window() change_direction() my_robot.move() my_robot.move() my_robot.turn_left() # Now close all the windows in the house!
5
1
TA
task2 The part where the eyes disappear is very novel, how did you squeeze it?
from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. global paper paper=Canvas(800,600,'light blue') global snows snows=Layer() for i in range(20): for j in range(20): snow=Circle(5,Point(70*i+10,-80*j)) snow.setFillColor('white') snows.add(snow) snows.moveTo(0,600) snows.setDepth(100) paper.add(snows) global man man=Layer() umbrella=Ellipse(170,120,Point(0,-200)) umbrella.setFillColor('Green') umbrella.setBorderColor('Green') umbrella.setDepth(80) man.add(umbrella) umbrella1=Rectangle(170,200,Point(0,-100)) umbrella1.setFillColor('light blue') umbrella1.setBorderColor('light blue') umbrella1.setDepth(70) man.add(umbrella1) bar=Rectangle(10,100,Point(0,-150)) bar.setFillColor('Black') bar.setBorderColor('Black') bar.setDepth(60) man.add(bar) hand=Rectangle(50,15,Point(-25,-140)) skin=(251,206,177) hand.setFillColor(skin) hand.setBorderColor(skin) hand.setDepth(60) man.add(hand) hand1=Rectangle(15,50,Point(-60,-120)) hand1.setFillColor(skin) hand1.setBorderColor(skin) hand1.setDepth(50) man.add(hand1) body=Rectangle(50,70,Point(-60,-125)) body.setFillColor('Yellow') body.setBorderColor('Yellow') body.setDepth(60) man.add(body) head=Circle(25,Point(-60,-185)) head.setFillColor(skin) head.setBorderColor(skin) head.setDepth(60) man.add(head) head1=Rectangle(50,10,Point(-60,-205)) head1.setFillColor('Green') head1.setBorderColor('Green') head1.setDepth(60) man.add(head1) leg1=Rectangle(15,60,Point(-47,-60)) leg1.setFillColor(skin) leg1.setBorderColor(skin) leg1.setDepth(50) man.add(leg1) leg2=Rectangle(15,60,Point(-73,-60)) leg2.setFillColor(skin) leg2.setBorderColor(skin) leg2.setDepth(50) man.add(leg2) man.moveTo(0,600) paper.add(man) def show_animation(): timeDelay=0 for i in range(80): snows.move(0,10) man.move(10,0) #sleep(timeDelay) pass draw_animal() show_animation()
88
90
code_explain
Covered with a rectangle of the same color as the background color
cs1qa
null
null
null
null
null
Question: task2 The part where the eyes disappear is very novel, how did you squeeze it? Code: from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. global paper paper=Canvas(800,600,'light blue') global snows snows=Layer() for i in range(20): for j in range(20): snow=Circle(5,Point(70*i+10,-80*j)) snow.setFillColor('white') snows.add(snow) snows.moveTo(0,600) snows.setDepth(100) paper.add(snows) global man man=Layer() umbrella=Ellipse(170,120,Point(0,-200)) umbrella.setFillColor('Green') umbrella.setBorderColor('Green') umbrella.setDepth(80) man.add(umbrella) umbrella1=Rectangle(170,200,Point(0,-100)) umbrella1.setFillColor('light blue') umbrella1.setBorderColor('light blue') umbrella1.setDepth(70) man.add(umbrella1) bar=Rectangle(10,100,Point(0,-150)) bar.setFillColor('Black') bar.setBorderColor('Black') bar.setDepth(60) man.add(bar) hand=Rectangle(50,15,Point(-25,-140)) skin=(251,206,177) hand.setFillColor(skin) hand.setBorderColor(skin) hand.setDepth(60) man.add(hand) hand1=Rectangle(15,50,Point(-60,-120)) hand1.setFillColor(skin) hand1.setBorderColor(skin) hand1.setDepth(50) man.add(hand1) body=Rectangle(50,70,Point(-60,-125)) body.setFillColor('Yellow') body.setBorderColor('Yellow') body.setDepth(60) man.add(body) head=Circle(25,Point(-60,-185)) head.setFillColor(skin) head.setBorderColor(skin) head.setDepth(60) man.add(head) head1=Rectangle(50,10,Point(-60,-205)) head1.setFillColor('Green') head1.setBorderColor('Green') head1.setDepth(60) man.add(head1) leg1=Rectangle(15,60,Point(-47,-60)) leg1.setFillColor(skin) leg1.setBorderColor(skin) leg1.setDepth(50) man.add(leg1) leg2=Rectangle(15,60,Point(-73,-60)) leg2.setFillColor(skin) leg2.setBorderColor(skin) leg2.setDepth(50) man.add(leg2) man.moveTo(0,600) paper.add(man) def show_animation(): timeDelay=0 for i in range(80): snows.move(0,10) man.move(10,0) #sleep(timeDelay) pass draw_animal() show_animation()
2
1
TA
Please explain the plant2 function to task2.
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=64) hubo.set_trace('blue') def move(): hubo.move() def cw(): for i in range(3): hubo.turn_left() def ccw(): hubo.turn_left() def pick(): hubo.pick_beeper() def harvest(): move() if hubo.on_beeper()==True: pick() def plant(): move() if hubo.on_beeper()==False: hubo.drop_beeper() def plant_line(): for i in range(5): plant() #harvest() plant() def plant_2(): plant_line() ccw() plant() ccw() plant_line() cw() for i in range (2): plant_2() plant() cw() plant_2()
22
26
variable
It is a code that goes to the right and picks up one space up and to the left.
cs1qa
null
null
null
null
null
Question: Please explain the plant2 function to task2. Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=64) hubo.set_trace('blue') def move(): hubo.move() def cw(): for i in range(3): hubo.turn_left() def ccw(): hubo.turn_left() def pick(): hubo.pick_beeper() def harvest(): move() if hubo.on_beeper()==True: pick() def plant(): move() if hubo.on_beeper()==False: hubo.drop_beeper() def plant_line(): for i in range(5): plant() #harvest() plant() def plant_2(): plant_line() ccw() plant() ccw() plant_line() cw() for i in range (2): plant_2() plant() cw() plant_2()
1
3
TA
Please explain each function
from cs1robots import * load_world("worlds/harvest1.wld") robot = Robot() robot.set_trace("blue") robot.set_pause(.1) def turn_right(): for i in range(3): robot.turn_left() def one_cycle(): for j in range(5): robot.move() robot.pick_beeper() robot.turn_left() robot.move() robot.pick_beeper() robot.turn_left() for k in range(5): robot.move() robot.pick_beeper() def set_next_position(): turn_right() robot.move() robot.pick_beeper() turn_right() robot.move() robot.pick_beeper() for l in range(2): one_cycle() set_next_position() one_cycle()
9
25
variable
one_cycle() is a function used to pick up two lines of beeper, and set_next_position() is a function used to move to the next line.
cs1qa
null
null
null
null
null
Question: Please explain each function Code: from cs1robots import * load_world("worlds/harvest1.wld") robot = Robot() robot.set_trace("blue") robot.set_pause(.1) def turn_right(): for i in range(3): robot.turn_left() def one_cycle(): for j in range(5): robot.move() robot.pick_beeper() robot.turn_left() robot.move() robot.pick_beeper() robot.turn_left() for k in range(5): robot.move() robot.pick_beeper() def set_next_position(): turn_right() robot.move() robot.pick_beeper() turn_right() robot.move() robot.pick_beeper() for l in range(2): one_cycle() set_next_position() one_cycle()
8
1
TA
What does the reader function do to the result?
from csv import * f=open('average-latitude-longitude-countries.csv','r') rdr=reader(f) list1=[] list2=[] dic={} next(rdr) for line in rdr: print(line) list1.append((line[0],line[1])) list2.append((line[0],(float(line[2]),float(line[3])))) dic[line[0]]=line[1] def act1(): print(list1) print(list2) def act2(): for i in range(len(list2)): if list2[i][1][0]<0: print(list1[i][1]) def act3(): cc=input('Enter country code: ') if cc in dic: print(dic.get(cc)) # act1() # act2() # act3()
3
3
variable
I know that, is divided into divisions
cs1qa
null
null
null
null
null
Question: What does the reader function do to the result? Code: from csv import * f=open('average-latitude-longitude-countries.csv','r') rdr=reader(f) list1=[] list2=[] dic={} next(rdr) for line in rdr: print(line) list1.append((line[0],line[1])) list2.append((line[0],(float(line[2]),float(line[3])))) dic[line[0]]=line[1] def act1(): print(list1) print(list2) def act2(): for i in range(len(list2)): if list2[i][1][0]<0: print(list1[i][1]) def act3(): cc=input('Enter country code: ') if cc in dic: print(dic.get(cc)) # act1() # act2() # act3()
9
1
TA
What information does an object of class Card in task2 have?
import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Card: def __init__(self, suit, face, state=True): self.suit=suit self.face=face for i in range(13): if self.face==face_names[i]: self.value=value[i] break self.state=state def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ deck=[] for suit in suit_names: for face in face_names: deck.append(Card(suit, face)) random.shuffle(deck) return deck def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ total=0 for card in hand: total=total+card.value return total def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ article="a " if card.face in [8, "Ace"]: article="an " return (article+str(card.face)+" of "+str(card.suit)) def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ answer=str(input(prompt)) if answer=="y": return True elif answer=="n": return False else: print("I beg your pardon!") ask_yesno(promt) def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 for i in range(len(dealer)): if dealer[i].state: imagename=str(dealer[i].suit)+"_"+str(dealer[i].face)+".png" img=Image(img_path+imagename) dealer[i].img=img else: img=Image(img_path+"Back.png") dealer[i].img=img for i in range(len(player)): if player[i].state: imagename=str(player[i].suit)+"_"+str(player[i].face)+".png" img=Image(img_path+imagename) player[i].img=img else: img=Image(img_path+"Back.png") player[i].img=img bj_board.clear() for i in range(len(dealer)): dealer[i].img.moveTo(x0, y0) dealer[i].img.setDepth(depth) bj_board.add(dealer[i].img) x0=x0+20 depth=depth-10 for i in range(len(player)): player[i].img.moveTo(x1, y1) player[i].img.setDepth(depth) bj_board.add(player[i].img) x1=x1+20 depth=depth-10 sum_dealer=hand_value(dealer) sum_player=hand_value(player) text_dealer="The dealer's Total : "+str(sum_dealer) text_player="The player's Total : "+str(sum_player) text1=Text(text_dealer, 20, Point(400, 100)) if dealer[0].state: bj_board.add(text1) text2=Text(text_player, 20, Point(400, 300)) bj_board.add(text2) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
17
20
variable
It is suit, face, value, image and state (determining whether it is hidden or not).
cs1qa
null
null
null
null
null
Question: What information does an object of class Card in task2 have? Code: import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Card: def __init__(self, suit, face, state=True): self.suit=suit self.face=face for i in range(13): if self.face==face_names[i]: self.value=value[i] break self.state=state def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ deck=[] for suit in suit_names: for face in face_names: deck.append(Card(suit, face)) random.shuffle(deck) return deck def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ total=0 for card in hand: total=total+card.value return total def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ article="a " if card.face in [8, "Ace"]: article="an " return (article+str(card.face)+" of "+str(card.suit)) def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ answer=str(input(prompt)) if answer=="y": return True elif answer=="n": return False else: print("I beg your pardon!") ask_yesno(promt) def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 for i in range(len(dealer)): if dealer[i].state: imagename=str(dealer[i].suit)+"_"+str(dealer[i].face)+".png" img=Image(img_path+imagename) dealer[i].img=img else: img=Image(img_path+"Back.png") dealer[i].img=img for i in range(len(player)): if player[i].state: imagename=str(player[i].suit)+"_"+str(player[i].face)+".png" img=Image(img_path+imagename) player[i].img=img else: img=Image(img_path+"Back.png") player[i].img=img bj_board.clear() for i in range(len(dealer)): dealer[i].img.moveTo(x0, y0) dealer[i].img.setDepth(depth) bj_board.add(dealer[i].img) x0=x0+20 depth=depth-10 for i in range(len(player)): player[i].img.moveTo(x1, y1) player[i].img.setDepth(depth) bj_board.add(player[i].img) x1=x1+20 depth=depth-10 sum_dealer=hand_value(dealer) sum_player=hand_value(player) text_dealer="The dealer's Total : "+str(sum_dealer) text_player="The player's Total : "+str(sum_player) text1=Text(text_dealer, 20, Point(400, 100)) if dealer[0].state: bj_board.add(text1) text2=Text(text_player, 20, Point(400, 300)) bj_board.add(text2) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
2
2
TA
Why did 3 suddenly use my_robot?
from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace('blue') # load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') def turn_right(): for i in range(3): my_robot.turn_left() def jump_one_hurdle(): my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() while not my_robot.on_beeper(): if my_robot.front_is_clear(): my_robot.move() else: jump_one_hurdle()
9
9
reasoning
I think I did it because my_robot was already written
cs1qa
null
null
null
null
null
Question: Why did 3 suddenly use my_robot? Code: from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace('blue') # load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') def turn_right(): for i in range(3): my_robot.turn_left() def jump_one_hurdle(): my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() while not my_robot.on_beeper(): if my_robot.front_is_clear(): my_robot.move() else: jump_one_hurdle()
3
3
TA
In task4, did you use x to check if you came to the original position??
from cs1robots import * load_world('worlds/rain2.wld') hubo = Robot(beepers=100, avenue=2, street=6, orientation='E') hubo.set_trace('blue') x=0 def check(): global x if hubo.on_beeper(): x=x+1 def turn_right(): for i in range(3): hubo.turn_left() def turn_around(): for j in range(2): hubo.turn_left() def move(): z=0 global x while x!=1: if hubo.right_is_clear(): hubo.move() if not hubo.right_is_clear(): z=z+1 turn_around() hubo.move() hubo.move() turn_around() if not hubo.right_is_clear(): z=z+1 hubo.move() if z==2: hubo.drop_beeper() hubo.move() check() else: turn_right() hubo.move() check() z=0 else: if hubo.front_is_clear(): hubo.move() check() else: hubo.turn_left() hubo.move() check() if x==1: hubo.pick_beeper() turn_right() hubo.move() hubo.drop_beeper() turn_right() hubo.move() check() move()
22
54
variable
I made sure we're back to the starting position
cs1qa
null
null
null
null
null
Question: In task4, did you use x to check if you came to the original position?? Code: from cs1robots import * load_world('worlds/rain2.wld') hubo = Robot(beepers=100, avenue=2, street=6, orientation='E') hubo.set_trace('blue') x=0 def check(): global x if hubo.on_beeper(): x=x+1 def turn_right(): for i in range(3): hubo.turn_left() def turn_around(): for j in range(2): hubo.turn_left() def move(): z=0 global x while x!=1: if hubo.right_is_clear(): hubo.move() if not hubo.right_is_clear(): z=z+1 turn_around() hubo.move() hubo.move() turn_around() if not hubo.right_is_clear(): z=z+1 hubo.move() if z==2: hubo.drop_beeper() hubo.move() check() else: turn_right() hubo.move() check() z=0 else: if hubo.front_is_clear(): hubo.move() check() else: hubo.turn_left() hubo.move() check() if x==1: hubo.pick_beeper() turn_right() hubo.move() hubo.drop_beeper() turn_right() hubo.move() check() move()
3
2
TA
What do the av and st variables do?
from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo=Robot() hubo.set_trace('blue') av=0 st=0 def turn_right(): for i in range (3): hubo.turn_left() while(hubo.front_is_clear()): hubo.move() av=av+1 hubo.turn_left() hubo.turn_left() for i in range (av): hubo.move() turn_right() while(hubo.front_is_clear()): hubo.move() st=st+1 hubo.turn_left() hubo.turn_left() for i in range (st): hubo.move() hubo.turn_left() def horizontalMove(): for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def goup(): hubo.turn_left() hubo.move() hubo.turn_left() for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() hubo.turn_left() for j in range (st): horizontalMove() goup() horizontalMove() turn_right() while hubo.front_is_clear(): hubo.move() turn_right() while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left()
8
60
variable
av is the number of horizontal cells -1, and st is the number of vertical cells -1.
cs1qa
null
null
null
null
null
Question: What do the av and st variables do? Code: from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo=Robot() hubo.set_trace('blue') av=0 st=0 def turn_right(): for i in range (3): hubo.turn_left() while(hubo.front_is_clear()): hubo.move() av=av+1 hubo.turn_left() hubo.turn_left() for i in range (av): hubo.move() turn_right() while(hubo.front_is_clear()): hubo.move() st=st+1 hubo.turn_left() hubo.turn_left() for i in range (st): hubo.move() hubo.turn_left() def horizontalMove(): for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def goup(): hubo.turn_left() hubo.move() hubo.turn_left() for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() hubo.turn_left() for j in range (st): horizontalMove() goup() horizontalMove() turn_right() while hubo.front_is_clear(): hubo.move() turn_right() while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left()
3
1
TA
Which part of task2 puts down all beepers?
from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash1.wld" ) # load_world( "worlds/trash2.wld" ) hubo = Robot() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def turn(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn() while hubo.front_is_clear(): hubo.move() turn_right() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn() hubo.move()
24
25
code_explain
This is a while statement that puts down the beeper until the carries_beepers() function returns false.
cs1qa
null
null
null
null
null
Question: Which part of task2 puts down all beepers? Code: from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash1.wld" ) # load_world( "worlds/trash2.wld" ) hubo = Robot() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def turn(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn() while hubo.front_is_clear(): hubo.move() turn_right() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn() hubo.move()
6
2
TA
How did you implement the two cards to show a picture when checking two cards?
from cs1graphics import * import random import time canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): if num1==num2: return False elif not 0<=num1<24: return False elif not 0<=num2<24: return False elif num1 in correct_list: return False elif num2 in correct_list: return False else: return True def check(num1, num2): global tries tries+=1 correct_list.append(num1) correct_list.append(num2) if cards[num1][1]==cards[num2][1]: print_cards() return True else: print_cards() correct_list.remove(num1) correct_list.remove(num2) print_cards() return False initialize() correct_list=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] print_cards() correct_list=[] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
81
91
code_explain
First I did print_cards() by adding the two numbers I entered into the correct_list, and then if the two were different cards, I removed the two numbers from the correct_list.
cs1qa
null
null
null
null
null
Question: How did you implement the two cards to show a picture when checking two cards? Code: from cs1graphics import * import random import time canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): if num1==num2: return False elif not 0<=num1<24: return False elif not 0<=num2<24: return False elif num1 in correct_list: return False elif num2 in correct_list: return False else: return True def check(num1, num2): global tries tries+=1 correct_list.append(num1) correct_list.append(num2) if cards[num1][1]==cards[num2][1]: print_cards() return True else: print_cards() correct_list.remove(num1) correct_list.remove(num2) print_cards() return False initialize() correct_list=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] print_cards() correct_list=[] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
1
0
TA
What is the zigzag function in task 1?
from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def zigzag(): hubo.turn_left() for i in range(9): hubo.move() for i in range(3): hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() for i in range(9): hubo.move() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
6
16
variable
It is a function that goes back and forth once from top to bottom.
cs1qa
null
null
null
null
null
Question: What is the zigzag function in task 1? Code: from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def zigzag(): hubo.turn_left() for i in range(9): hubo.move() for i in range(3): hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() for i in range(9): hubo.move() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
2
3
TA
Explain the farmer function in task 4
from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def harvest(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() while hubo.front_is_clear(): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() def farmer(): for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() turn_right() harvest() turn_right() for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() farmer() farmer() for i in range(5): harvest()
24
38
variable
And the farmer function is defined to pick up the beeper by continuing to execute the harvest function, a function that picks up the beeper, while moving in zigzag while moving one line to the right and one line to the left as a unit.
cs1qa
null
null
null
null
null
Question: Explain the farmer function in task 4 Code: from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def harvest(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() while hubo.front_is_clear(): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() def farmer(): for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() turn_right() harvest() turn_right() for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() farmer() farmer() for i in range(5): harvest()
2
0
TA
What is harvest() function in task1?
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def harvest(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def curve(): for i in range(5): harvest() hubo.turn_left() harvest() hubo.turn_left() for i in range(5): harvest() turn_right() harvest() for i in range(2): curve() harvest() turn_right() curve()
7
10
variable
Moving After checking if there is a beeper Is a pick function
cs1qa
null
null
null
null
null
Question: What is harvest() function in task1? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def harvest(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def curve(): for i in range(5): harvest() hubo.turn_left() harvest() hubo.turn_left() for i in range(5): harvest() turn_right() harvest() for i in range(2): curve() harvest() turn_right() curve()
2
0
TA
What is set2 function in task1?
from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def move_and_pick(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def set1(): if hubo.on_beeper(): hubo.pick_beeper() for i in range(5): move_and_pick() def set2(): set1() hubo.turn_left() move_and_pick() hubo.turn_left() set1() turn_right() hubo.move() set2() for i in range(2): hubo.move() turn_right() set2() hubo.turn_left()
23
29
variable
I created a function for two lines by combining two set1 functions that define the movement of a robot and a direction change function.
cs1qa
null
null
null
null
null
Question: What is set2 function in task1? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def move_and_pick(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def set1(): if hubo.on_beeper(): hubo.pick_beeper() for i in range(5): move_and_pick() def set2(): set1() hubo.turn_left() move_and_pick() hubo.turn_left() set1() turn_right() hubo.move() set2() for i in range(2): hubo.move() turn_right() set2() hubo.turn_left()
4
1
TA
When does the is_triangle() function return True in Task2?
def is_triangle(a, b, c): if a + b > c : if a + c > b : if b + c > a : print("YES") else : print("NO") else : print("NO") else : print("NO") return False a = float(input("Side a:")) b = float(input("Side b:")) c = float(input("Side c:")) is_triangle(a, b, c)
1
12
variable
I know it returns True when the print function is executed according to the conditional statement.
cs1qa
null
null
null
null
null
Question: When does the is_triangle() function return True in Task2? Code: def is_triangle(a, b, c): if a + b > c : if a + c > b : if b + c > a : print("YES") else : print("NO") else : print("NO") else : print("NO") return False a = float(input("Side a:")) b = float(input("Side b:")) c = float(input("Side c:")) is_triangle(a, b, c)
9
0
student
what are s and v for?
# Copy your "Memento" code from the task in Lab 6. from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = range(24) class Card: def _init_(self): pass def initialize(): # initialize cards for i in range(6): for k in range(4): ncard = Card() ncard.img = Image(path + names[i]) ncard.name = names[i] # img = Image(path + names[i]) # temp_tuple = (img, names[i]) cards.append(ncard) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h + h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h + h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 != num2: if num2 not in correct_list: if num1 not in correct_list: if 0 <= num1 <= 23: if 0 <= num2 <= 23: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1] == cards[num2][1]: print_cards() return True correct_list.remove(num2) correct_list.remove(num1) print_cards() return False initialize() print_cards() time.sleep(3) correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list) // 2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): tries = tries + 1 continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ##################################################################
null
null
code_understanding
that is just sample like name you should change according to your codes
cs1qa
null
null
null
null
null
Question: what are s and v for? Code: # Copy your "Memento" code from the task in Lab 6. from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = range(24) class Card: def _init_(self): pass def initialize(): # initialize cards for i in range(6): for k in range(4): ncard = Card() ncard.img = Image(path + names[i]) ncard.name = names[i] # img = Image(path + names[i]) # temp_tuple = (img, names[i]) cards.append(ncard) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h + h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h + h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 != num2: if num2 not in correct_list: if num1 not in correct_list: if 0 <= num1 <= 23: if 0 <= num2 <= 23: return True return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1] == cards[num2][1]: print_cards() return True correct_list.remove(num2) correct_list.remove(num1) print_cards() return False initialize() print_cards() time.sleep(3) correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list) // 2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): tries = tries + 1 continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ##################################################################
6
1
TA
Please explain number 2 for each function
import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ li=[] for i in range(trials): a = random.randrange(lb, ub+1) li.append(a) return li def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ n = len(num_list) a = 0 avg = num_list.copy() for i in range(n): a = a + avg.pop() b = a/n return b def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ count = [] copy = num_list.copy() copy.sort() a = copy.pop(0) b = copy.pop() for i in range(b-a+1): c = num_list.count(a+i) count.append((a+i, c)) return count # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
18
30
variable
average_integers() adds each term to the list created above and divides it by the length of the list to find the average.
cs1qa
null
null
null
null
null
Question: Please explain number 2 for each function Code: import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ li=[] for i in range(trials): a = random.randrange(lb, ub+1) li.append(a) return li def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ n = len(num_list) a = 0 avg = num_list.copy() for i in range(n): a = a + avg.pop() b = a/n return b def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ count = [] copy = num_list.copy() copy.sort() a = copy.pop(0) b = copy.pop() for i in range(b-a+1): c = num_list.count(a+i) count.append((a+i, c)) return count # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
2
4
TA
What is the role of t in your code?
from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=6) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... hubo=Robot(); hubo.set_trace('blue'); t=0; def turn_right(): for i in range(3): hubo.turn_left(); def go_straight(): while hubo.front_is_clear(): hubo.move(); def meet_left_wall(): turn_right(); hubo.move(); turn_right(); def meet_right_wall(): hubo.turn_left(); hubo.move(); hubo.turn_left(); hubo.turn_left(); go_straight(); while hubo.front_is_clear() or hubo.left_is_clear() or hubo.right_is_clear(): if t and not hubo.front_is_clear() and not (hubo.left_is_clear() and hubo.right_is_clear()): break; t=t+1; if not hubo.front_is_clear(): if t%2: meet_left_wall(); else : meet_right_wall(); go_straight();
37
47
variable
It is a variable indicating how far the vertical line has gone, Except for mx1, it excludes the first left corner To be able to finish when you get to the right corner!
cs1qa
null
null
null
null
null
Question: What is the role of t in your code? Code: from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=6) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... hubo=Robot(); hubo.set_trace('blue'); t=0; def turn_right(): for i in range(3): hubo.turn_left(); def go_straight(): while hubo.front_is_clear(): hubo.move(); def meet_left_wall(): turn_right(); hubo.move(); turn_right(); def meet_right_wall(): hubo.turn_left(); hubo.move(); hubo.turn_left(); hubo.turn_left(); go_straight(); while hubo.front_is_clear() or hubo.left_is_clear() or hubo.right_is_clear(): if t and not hubo.front_is_clear() and not (hubo.left_is_clear() and hubo.right_is_clear()): break; t=t+1; if not hubo.front_is_clear(): if t%2: meet_left_wall(); else : meet_right_wall(); go_straight();
5
0
TA
What is the pick_up() function in task1?
from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def hubo_turn_right(): for i in range(3): hubo.turn_left() def pick_up(): for i in range(5): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def turn1(): hubo.turn_left() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() def turn2(): hubo_turn_right() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo_turn_right() hubo.move() hubo.pick_beeper() for i in range(2): pick_up() turn1() pick_up() turn2() pick_up() turn1() pick_up()
9
13
variable
This is a function that picks up the beeper if the robot goes forward and is on the beeper.
cs1qa
null
null
null
null
null
Question: What is the pick_up() function in task1? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def hubo_turn_right(): for i in range(3): hubo.turn_left() def pick_up(): for i in range(5): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def turn1(): hubo.turn_left() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() def turn2(): hubo_turn_right() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo_turn_right() hubo.move() hubo.pick_beeper() for i in range(2): pick_up() turn1() pick_up() turn2() pick_up() turn1() pick_up()
5
1
TA
Can you explain what animation you used to make the objects sound in Task2?
from cs1graphics import * from time import sleep def draw_animal(): canvas = Canvas(800,500) canvas.setBackgroundColor('blue') global sq sq = Square(120) canvas.add(sq) sq.setFillColor('yellow') sq.moveTo(400,200) global cir1 global cir2 cir1 = Circle(10) cir2 = Circle(10) cir1.setFillColor('black') cir2.setFillColor('black') canvas.add(cir1) canvas.add(cir2) cir1.moveTo(370,180) cir2.moveTo(430,180) global cir3 cir3 = Circle(20) canvas.add(cir3) cir3.setFillColor('pink') cir3.moveTo(400,230) global rec1 global rec2 rec1 = Rectangle(10,100) rec2 = Rectangle(10,100) canvas.add(rec1) canvas.add(rec2) rec1.moveTo(340,200) rec2.moveTo(460,200) rec1.setFillColor('yellow') rec2.setFillColor('yellow') rec1.setBorderColor('yellow') rec2.setBorderColor('yellow') sq.setBorderColor('yellow') cir3.setBorderColor('pink') global d d=Layer() d.add(sq) d.add(cir1) d.add(cir2) d.add(cir3) d.add(rec1) d.add(rec2) # Implement this function. pass def show_animation(): for i in range(45): sq.move(1,0) cir1.move(1,0) cir2.move(1,0) cir3.move(1,0) rec1.move(1,0) rec2.move(1,0) rec1.rotate(-1) rec2.rotate(1) sleep(0.1) # Implement this function. pass draw_animal() show_animation()
51
61
code_explain
I used move and rotate
cs1qa
null
null
null
null
null
Question: Can you explain what animation you used to make the objects sound in Task2? Code: from cs1graphics import * from time import sleep def draw_animal(): canvas = Canvas(800,500) canvas.setBackgroundColor('blue') global sq sq = Square(120) canvas.add(sq) sq.setFillColor('yellow') sq.moveTo(400,200) global cir1 global cir2 cir1 = Circle(10) cir2 = Circle(10) cir1.setFillColor('black') cir2.setFillColor('black') canvas.add(cir1) canvas.add(cir2) cir1.moveTo(370,180) cir2.moveTo(430,180) global cir3 cir3 = Circle(20) canvas.add(cir3) cir3.setFillColor('pink') cir3.moveTo(400,230) global rec1 global rec2 rec1 = Rectangle(10,100) rec2 = Rectangle(10,100) canvas.add(rec1) canvas.add(rec2) rec1.moveTo(340,200) rec2.moveTo(460,200) rec1.setFillColor('yellow') rec2.setFillColor('yellow') rec1.setBorderColor('yellow') rec2.setBorderColor('yellow') sq.setBorderColor('yellow') cir3.setBorderColor('pink') global d d=Layer() d.add(sq) d.add(cir1) d.add(cir2) d.add(cir3) d.add(rec1) d.add(rec2) # Implement this function. pass def show_animation(): for i in range(45): sq.move(1,0) cir1.move(1,0) cir2.move(1,0) cir3.move(1,0) rec1.move(1,0) rec2.move(1,0) rec1.rotate(-1) rec2.rotate(1) sleep(0.1) # Implement this function. pass draw_animal() show_animation()