# 0 |-|-|-|-| # 1 |-|-|-|-| # 2 |-|-|-|-| # 3 |-|-|-|-| output = "" # Column index header: row_size = ll.length(new_board) output += " " for col_index in range(0, row_size): output += str(col_index) + " " output += "\n" # Rows to string: for row in new_board: (row_index, _, d) = ll.head(row) rest_rows = ll.tail(row) row_str = "|" + d for (r, c, d) in rest_rows: cell = "|" + d row_str += cell # output = output + cell if row_index < 10: row_str = " " + str(row_index) + " " + row_str + "|\n" else: row_str = str(row_index) + " " + row_str + "|\n" output += row_str print(output) # Redo do this problem using a list comprehension.
import list_lib as ll # Write the following list comprehension using an explicit for-loop: # # squares = [x**2 for x in range(0,43)] # # Then write a for-loop to print the list out to the screen in list format. squares = [] for x in range(0,43): squares = ll.cons(x**2,squares) squares = ll.reverse(squares) output = "[" for x in range(0,ll.length(squares) - 1): next_element = str(ll.head(squares)) squares = ll.tail(squares) output = output + next_element + ", " last_element = str(ll.head(squares)) output = output + last_element + "]" print(output)
## if nums_sum > 50: ## break ## nums_len -= 1 ##print("The sum is "+str(nums_sum)) for x in nums: print(str(x)) print(nums) print("-----------------------------") x = 0 proc_nums = nums while nums_len > 0: x = ll.head(proc_nums) proc_nums = ll.tail(proc_nums) print(str(x)) nums_len -= 1 print(nums) ##power_off = false ##while True: ## ... ## if power_off: ## break ##while !stopButtonUp: ## ... # Continue : Ends the iteration early (forces loop to return to the condition).
("Stephanie", "Doe", 50505050), ("Andy", "Bernard", 606060606)] new_students = [] length = ll.length(students) ##while length > 0: ## (first, last, sid) = ll.head(students) ## students = ll.tail(students) ## length -= 1 ## ## if first == "Stephanie" and last == "Doe": ## continue ## else: ## sid += 1 ## new_students = ll.cons((first, last, sid),new_students) first_name = "Pam" last_name = "Beesly" found_sid = 0 while length > 0: (first, last, sid) = ll.head(students) students = ll.tail(students) length -= 1 if first == first_name and last == last_name: found_sid = sid break # stop looking now! print("Still looking..") print("Found: " + str(found_sid))