0% found this document useful (0 votes)
75 views14 pages

SE 100 Assignment 6

The document is an assignment for a programming course. It contains 15 questions related to programming concepts like lists, loops, functions, and two-dimensional lists. The questions involve writing code to manipulate lists, iterate through lists, define functions, and check properties of two-dimensional lists. Students are asked to write code solutions for the questions and provide screenshots of outputs for questions that produce sample outputs.

Uploaded by

M.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views14 pages

SE 100 Assignment 6

The document is an assignment for a programming course. It contains 15 questions related to programming concepts like lists, loops, functions, and two-dimensional lists. The questions involve writing code to manipulate lists, iterate through lists, define functions, and check properties of two-dimensional lists. Students are asked to write code solutions for the questions and provide screenshots of outputs for questions that produce sample outputs.

Uploaded by

M.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Alfaisal University - College of Engineering

Software Engineering Department

Subject: SE 100 – Programming for Engineers


Assignment 6 (Fall 2023-2024)
Instructors Eng. Sarra Drine, Dr. Ahmad Sawalmeh, Eng. Safia Dawood, Dr. Lulwah Al-
Barrak
Deadline Sunday, 12-Nov-2023 @11:59PM
Grade Percentage 3 percent
Student Name: Omar Hussam Alotary

Student ID: 230645

Student Section: M5

Question 1: Write a statement that creates a list with the following strings: 'Einstein',
'Newton', 'Copernicus', and 'Kepler'.

scientists = ['Einstein', 'Newton', 'Copernicus', 'Kepler']

Question 2: Assume names references a list. Write a for loop that displays each element of the list.

names = ['Einstein', 'Newton', 'Copernicus', 'Kepler']

for name in names:


print(name)

Question 3: Assume the list numbers1 has 100 elements, and numbers2 is an empty list. Write
code that copies the values in numbers1 to numbers2.

numbers1 = [1, 2, 3, ...]

numbers2 = []

for num in numbers1:

numbers2.append(num)
SE 100 – Assignment 6 – Fall 2023-2024

Question 4: Draw a flowchart showing the general logic for totaling the values in a list.

Start

Start the
accumulator

Check if
there is Yes
another Read the next Add the number to
number in number the accumulator
the list

No

End

Question 5: Write a function that accepts a list as an argument (assume the list contains integers)
and returns the total of the values in the list.

def calculate_total(input_list):

total = 0

for num in input_list:

Page 2 of 14
SE 100 – Assignment 6 – Fall 2023-2024

total += num

return total

my_list = [1, 2, 3, 4, 5]

result = calculate_total(my_list)

print("The total is:", result)

Question 6: Assume the names variable references a list of strings. Write code that determines
whether 'Ruby' is in the names list. If it is, display the message 'Hello Ruby'. Otherwise,
display the message 'No Ruby'.

names = ['Einstein', 'Newton', 'Copernicus', 'Kepler']

if 'Ruby' in names:
print('Hello Ruby')
else:
print('No Ruby')

Page 3 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Question 7: What will the following code print?


list1 = [40, 50, 60]
list2 = [10, 20, 30]
list3 = list1 + list2
print(list3)

[40,50,60,10,20,30]

Question 8: Assume list1 is a list of integers. Write a statement that uses a list comprehension to
create a second list containing the squares of the elements of list1.

list1 = [1, 2, 3, 4, 5]
squared_list = [x ** 2 for x in list1]

Question 9: Assume list1 is a list of integers. Write a statement that uses a list comprehension to
create a second list containing the elements of list1 that are greater than 100.

list1 = [101, 50, 200, 80, 300]


greater_than_100_list = [x for x in list1 if x > 100]

Question 10: Assume list1 is a list of integers. Write a statement that uses a list comprehension
to create a second list containing the elements of list1 that are even numbers.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


even_numbers_list = [x for x in list1 if x % 2 == 0]

Question 11: Write a statement that creates a two-dimensional list with 5 rows and 3 columns. Then
write nested loops that get an integer value from the user for each element in the list.

matrix = [[0 for _ in range(3)] for _ in range(5)]

for row in range(5):

for col in range(3):

while True:

value = input(f"Enter the value for element at row {row+1}, column {col+1}: ")

Page 4 of 14
SE 100 – Assignment 6 – Fall 2023-2024

if value.isdigit():

matrix[row][col] = int(value)

break

else:

print("Please enter a valid integer.")

for row in matrix:

print(row)

Page 5 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Question 12 - Lottery Number Generator: Design a program that generates a seven-digit lottery
number. The program should generate seven random numbers, each in the range of 0 through 9,
and assign each number to a list element. Then write another loop that displays the numbers
separated by commas in a single line.
Write the code here and attach a screenshot of the output.

import random

lottery_numbers = []

for _ in range(7):

random_number = random.randint(0, 9)

lottery_numbers.append(random_number)

lottery_number_str = ', '.join(map(str, lottery_numbers))

print("Lottery Number:", lottery_number_str)

Page 6 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Question 13 - Rainfall Statistics: Design a program that lets the user enter the total rainfall for each
of 12 months into a list. The program should calculate and display the total rainfall for the year, the
average monthly rainfall, and the months with the highest and lowest amounts.

Write the code here and attach a screenshot of the output using some sample data as an example.
monthly_rainfall = []

for month in range(1, 13):

valid_input = False

while not valid_input:

rainfall_input = input(f"Enter the total rainfall for month {month}: ")

if rainfall_input.replace(".", "", 1).isdigit():

rainfall = float(rainfall_input)

monthly_rainfall.append(rainfall)

valid_input = True

else:

print("Please enter a valid number.")

total_rainfall = sum(monthly_rainfall)

average_rainfall = total_rainfall / 12

max_rainfall_month = monthly_rainfall.index(max(monthly_rainfall)) + 1

min_rainfall_month = monthly_rainfall.index(min(monthly_rainfall)) + 1

print(f"Total Rainfall for the Year: {total_rainfall} inches")

print(f"Average Monthly Rainfall: {average_rainfall:.2f} inches")

print(f"Month with Highest Rainfall: Month {max_rainfall_month}")

print(f"Month with Lowest Rainfall: Month {min_rainfall_month}")

Page 7 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Question 14 - Number Analysis Program: Design a program that asks the user to enter a series of 20
numbers. The program should store the numbers in a list then display the following data:

 The lowest number in the list


 The highest number in the list
 The total of the numbers in the list
 The average of the numbers in the list

Write the code here and attach a screenshot of the output using some sample data as an example.
numbers = []

for i in range(20):

valid_input = False

while not valid_input:

num_input = input(f"Enter number {i + 1}: ")

if num_input.replace(".", "", 1).isdigit():

num = float(num_input)

numbers.append(num)

valid_input = True

else:

print("Please enter a valid number.")

lowest = min(numbers)

highest = max(numbers)

total = sum(numbers)

average = total / 20

print(f"Lowest Number: {lowest}")

print(f"Highest Number: {highest}")

print(f"Total of Numbers: {total}")

print(f"Average of Numbers: {average:.2f}")

Page 8 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Page 9 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Question 15 - Larger Than n: In a program, write a function that accepts two arguments: a list, and a
number n. Assume that the list contains numbers. The function should display all of the numbers in
the list that are greater than the number n.

Write the code here and attach a screenshot of the output.

def display_numbers_greater_than_n(input_list, n):

for num in input_list:

if num > n:

print(num)

my_list = [1, 5, 10, 15, 20, 25, 30]

n = 15

display_numbers_greater_than_n(my_list, n)

Page 10 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Question 16 - Lo Shu Magic Square: The Lo Shu Magic Square is a grid with 3 rows and 3 columns.
The Lo Shu Magic Square has the following properties:

 The grid contains the numbers 1 through 9 exactly. Example:

 The sum of each row, each column, and each diagonal all add up to the same number.
Example:

In a program you can simulate a magic square using a two-dimensional list. Write a function that
accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu Magic
Square. Test the function in a program.

Write the code here and attach a screenshot of the output.

def is_loshu_magic_square(grid):
if len(grid) != 3 or len(grid[0]) != 3:
return False

row_sum = sum(grid[0])

for i in range(3):
if sum(grid[i]) != row_sum:
return False

for j in range(3):
col_sum = grid[0][j] + grid[1][j] + grid[2][j]
if col_sum != row_sum:
return False

main_diag_sum = grid[0][0] + grid[1][1] + grid[2][2]


anti_diag_sum = grid[0][2] + grid[1][1] + grid[2][0]

Page 11 of 14
SE 100 – Assignment 6 – Fall 2023-2024

return main_diag_sum == anti_diag_sum == row_sum

magic_square = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]


not_magic_square = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

if is_loshu_magic_square(magic_square):
print("The grid is a Lo Shu Magic Square.")
else:
print("The grid is not a Lo Shu Magic Square.")

if is_loshu_magic_square(not_magic_square):
print("The grid is a Lo Shu Magic Square.")
else:
print("The grid is not a Lo Shu Magic Square.")

Page 12 of 14
SE 100 – Assignment 6 – Fall 2023-2024

Question 17 - Prime Number Generation: A positive integer greater than 1 is said to be prime if it
has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime.
Write a program that asks the user to enter an integer greater than 1, then displays all of the prime
numbers that are less than or equal to the number entered. The program should work as follows:

 Once the user has entered a number, the program should populate a list with all of the
integers from 2 up through the value entered.
 The program should then use a loop to step through the list. The loop should pass each
element to a function that displays whether the element is a prime number, or a composite
number.

Write the code here and attach a screenshot of the output using some sample data as an example.

def is_prime(num):

if num <= 1:

return False

if num <= 3:

return True

if num % 2 == 0 or num % 3 == 0:

return False

i=5

while i * i <= num:

if num % i == 0 or num % (i + 2) == 0:

return False

i += 6

return True

def display_prime_numbers(limit):

prime_numbers = []

for num in range(2, limit + 1):

if is_prime(num):

prime_numbers.append(num)

return prime_numbers

n = input("Enter an integer greater than 1: ")


Page 13 of 14
SE 100 – Assignment 6 – Fall 2023-2024

if n.isdigit():

n = int(n)

if n <= 1:

print("Please enter an integer greater than 1.")

else:

prime_numbers = display_prime_numbers(n)

if prime_numbers:

print("Prime numbers less than or equal to", n, "are:", prime_numbers)

else:

print("No prime numbers found less than or equal to", n)

else:

print("Invalid input. Please enter a valid integer greater than 1.")

Page 14 of 14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy