New Assignment 1 2025
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
New Assignment 1 2025
Question 1: List Comprehension (1 Point)
Write a one-liner (one line of code) list comprehension that keeps only the perfect squares numbers from 1 to 100.
• Expected output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [ ]: #Question 1: Your code here
Question 2: Staircase Number Pattern (1 Point)
• Use a for loop to print a pyramid-like pattern with numbers.
• Each row should contain increasing numbers up to the row number. For example, with n-5, show the following result:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Show the result when n is 9
• Hint: you may need a nested for loop for this task
In [ ]: n=9
# Question 2: Your Code here
Question 3: Repeated Digits (1 Point)
For a number n, use a while loop to detect if there are any repeated digits in it.
• If there is no repeating digit, print ('No Repeating Digits Found!")
• Otherwise, print('Repeating Digits Found!")
Test when n=12345678
• Hint: you may need a 'flag' to indicate if there are any repeated digits in n.
In [ ]: n=12345678
# Question 3: Your code here
Question 4: Finding Unique Words in a Sentence (1 Point)
Display all unique words in a sorted list from the following string:
• "The quick brown fox jumps over the lazy dog and the dog jumps over the yellow fox head"
In [ ]: s="The quick brown fox jumps over the lazy dog and the dog jumps over the yellow fox he
# Question 4: Your code here
Question 5: Remove Duplicates from a List While Maintaining Order (1 Point)
Remove duplicate values from a list but keep the first occurrence of each element.
Input: m=[4.5,2.2.2.3.5,3,5,6,7,8,2]
Output: [4,5, 2, 3,6,7,8]
In [ ]: m=145.2.22 35.3.5. 6.7.82
# Question 5: Your code here
Question 6: Find Missing Number in a Sequence (1 Point)
You are given a list of numbers from 1 to n with one missing number. Write a Function to find the missing number.
Test Case 1:
Input: [1, 2, 3, 4, 6, 7, 8, 9, 10]
Output: 5
Test Case 2:
Input: [1, 2, 3, 5, 6, 7, 8, 9, 10]
Output: 4
Test Case 3:
Input: [1, 2, 3, 4, 5]
Output: 6
Your function must pass all test cases.
In [ ]: def checkmiss (num):
# Question 6: Your code here
In [ ]: checkmiss ([1, 2, 3, 4, 6, 7, 8, 9, 10])
In [ ]: checkmiss ([1, 2, 3, 5, 6,7, 8, 9, 10])
In [ ]: checkmiss ([1, 2, 3, 4, 5])
Question 7 Happy Numbers (1 Point)
A Happy Number is defined as follows:
• Start with any positive integer n.
• Replace n with the sum of the squares of its digits.
• Repeat the process.
• If it eventually reaches 1, it's a Happy Number.
• If it enters a cycle, it is not a Happy Number.
• Example:
19 → 12 + 92 = 82 →82 + 22 = 68 → 62 + 82 = 100 →12 + 02 + 02 = 1 (Hаppy!)
Your task: Define a function to detect if a number is a happy number or not. If it is a Happy Number, return True; otherwise, return False.
Then display all happy numbers from 1 to 100 in a list, using the function you defined.
In [ ]: ### Question 7: Your code here
2025-10-09