Python代写:CS1591 Computational Thinking

Introduction

代写四个Python小程序,start code已经给了,继续完善即可。

Problem Set 1 - Correcting Code

  • Correct the following code, the function sum_list is intended to return the sum of the elements in the list. Add your corrected code to your submission file.

    1
    2
    3
    4
    5
    6
    def sum_list(alist):
    for i in alist:
    sum = i
    return sum
    mylist = [45, 2, 10, -5, 100]
    print(sum_list(mylist))
  • Correct the following code. Add your corrected code to your submission file.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    def get_user_choice():
    while True:
    command = input("Command: ")
    if command = f or command = m or command = s or command = d or command = q:
    return command
    print("Hey, that's not a command. Here are your options:")
    print("f - Full speed ahead")
    print("m - Moderate speed")
    print("s - Status")
    print("d - Drink")
    print("q - Quit")
    user_command = get_user_choice()
    print("You entered: " + user_command)
  • Correct the following code: (This almost reverses the string. What is wrong?) Add your corrected code to your submission file.

    1
    2
    3
    4
    5
    6
    7
    8
    def reverse(text):
    result = ""
    text_length = len(text)
    for i in range(text_length):
    result = result + text[i * -1]
    return result
    text = "Programming is the coolest thing ever."
    print(reverse(text))

Problem Set 2 - Using loops

  • Using loops, write code that will print the following:
0 1 2 3 4 5 6 7 8 9
         0
0 1 2 3 4 5 6 7 8 9
         0
0 1 2 3 4 5 6 7 8 9
         0
0 1 2 3 4 5 6 7 8 9
         0
0 1 2 3 4 5 6 7 8 9
         0
0 1 2 3 4 5 6 7 8 9
  • Using loops, write code that will print the following:
0 1 2 3 4 5 6 7 8 9
  0 1 2 3 4 5 6 7 8
    0 1 2 3 4 5 6 7
      0 1 2 3 4 5 6
        0 1 2 3 4 5
          0 1 2 3 4
            0 1 2 3
              0 1 2
                0 1
                  0

Hint: Two loops are needed inside the outer loop that controls each row. First, a loop prints spaces, then a loop prints the numbers. Loop both of these for each row.

Problem Set 3 - Functions using sequences

To complete these exercises, you can use any of the programming constructs that we have seen in lectures, including built-in Python sequence and list operations, functions and methods.

  • Write a function that takes a list, modifies it by removing the first and last elements, and returns None.

    1
    defchop(mylist):
  • Add the code for your function to your submission file and then add the following code to demonstrate that your function works as required.

    1
    2
    3
    4
    5
    mylist = [1,3,4,5,6,7]
    print(mylist)
    chop(mylist)
    print(mylist)
    print(chop(mylist))
  • Write a function that, when given a list of items, returns a new list that contains every second element from the original list, that is, every element that has even index. Recall that a number n is even if n “mod” 2 is zero. The “mod” operator in Python is wriWen as %.

    1
    def evens(mylist):
  • for example: evens([a, b, c, d, e]) should return [a, c, e].
    Call your evens function on the argument [[“me”,”my”],[“you”,”yours”],[“them”],[“their”],[“theirs”]] and print
    out the result.
    Add the code for your function and your function call/print statement to your submission file.

  • Write a function inAlphabetOrder that returns True if the leWers in a word appear in alphabetical order (double leWers are ok). For inAlphabetOrder we have to compare adjacent leWers, which is a liWle tricky with a for loop. Try using a while loop.

    1
    def inAlphabetOrder(myword):
  • for example: inAlphabetOrder(“loop”) should inAlphabetOrder(“baby”) should return False.
    Call your inAlphabetOrder function on the arguments “loops”, “looping” and “loopy” and print out the results. Add the code for your inAlphabetOrder function and your example function calls/print statements to your submission file.

Problem Set 4 - Using list comprehensions

  • Using a list comprehension, write a function squares_list that returns a list of the squares of all the even numbers in the range 0-99.
    Print out the result of calling this function. Add the code for your squares_list function and your function call/print statement to your submission file.
  • Using a list comprehension, write a function wordlengths that takes a list of words as its argument and returns a list of pairs containing 1. the capitalised word and 2. the length of the word.

    1
    def wordlengths(mywords):
  • for example: wordlengths([“The”, “quick”, “brown”, “fox”]) should return [(“THE”, 3), (“QUICK”, 5), (“BROWN”, 5), (“FOX”, 3)]
    Add the code for your wordlengths function and a demonstration function call/print statement to your submission file.
    Complete as many of these problems as you can. Partial solutions will be given partial credit.