for (r, c, d) in row:
        if c % 2 != 0:
            new_row = ll.append(new_row, [(r, c, 'X')])
        else:
            new_row = ll.append(new_row, [(r, c, d)])
    new_board = ll.append(new_board, [new_row])

#     0 1 2 3
#  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"
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)
##n = 5
##while n >= 0:
##    if n % 2 == 0:
##        print("n = "+str(n))
##        print("Hello, pizza, mouth, now!")
##    n -= 1

##n = 25
##while n < 30:
##    print("Hello")
##    n += 1

# Take the sum of nums using a while-loop:
nums = [5, 26, 42, 25, 30, 50]
nums_len = ll.length(nums)
nums_sum = 0
##while nums_len > 0:
##    n = ll.head(nums)
##    nums = ll.tail(nums)
##    nums_sum += n
##    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("-----------------------------")
Пример #4
0
import list_lib as ll

# Continue: Given the list of students add one to the each of their
# student ids, and remove Stephanie from the list.

students = [("Jim", "Halport", 10101010), ("Michael", "Scott", 2020202),
            ("Pam", "Beesly", 30303030), ("Dwight", "Schrute", 40404040),
            ("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