CSCI101 Lab04 AdvancedSelection (1)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

University of Science and Technology

Communications & Information Engineering Program


CSCI 101: Introduction to Computer Science
Advanced Selection

Note: Problems 1-2 should be solved on paper then checked on computer


1. What is the output of the following pieces of code:
x=5 for x in 'zewail city':
y = -10 if x in 'snow white':
If x > 0 : print(x)
If y > 0 :
print(‘first option’) #What if we exchanged the two strings
else : #above? Would the output be the same?
print(‘second option’)
else: for x in 'snow white':
If y > 0 : if x in 'zewail city':
print(‘third option’) print(x)
else:
print(‘fourth option’)
Part a Part b

2. Ali wants to buy new sports shoes, his mother told him he should finish his
assignment first. Also she said if the shoes are +900 LE his dad will pay otherwise
she will.
● First think of this algorithm overall
● Complete the following :
Finished_Assignment = input(‘Ali did you finish your assignment ? ’)
Shoes_Price = float(input(‘what is the shoes price? ’))
If __________________:
If____________:
print(‘the shoe price is’,___ ,’and dad will pay’)
else:
print(‘the shoe price is’,___ ,’and mom will pay’)
else:
print(_________) #print what represents this case

Page 1
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Advanced Selection

3. Navy had a project with her colleagues, she waited until all team members attend
and see if anyone brought the battery otherwise she would bring it for them to start
working. Solve without using loops
Sample Run 1* Sample Run 2*

What did team members bring? What did team members bring?
Arduino, capacitors, breadboard, leds Arduino, battery, breadboard, leds
I will need to bring the battery I will need to bring the battery
*User input is shown in bold font

4. Write a program to read three numbers from the user and print them in ascending
order.

5. Write a program to convert the weight from mg, Kg, or Ton to gram upon the user
choice. If the user enters a negative weight, the program prints an error message
(Invalid weight). If the user enters a wrong selection, the program prints an error
message (Invalid unit). The program should be executed exactly 5 times.

Sample runs*
Sample Run 1 Enter weight: 10
Enter weight unit (1 for mg, 2 for kg, 3 for ton): 2
Converting kg to gram
10 kg = 10000 gram
Sample Run 2 Enter weight: -5
Invalid weight
Sample Run 3 Enter weight: 5
Enter weight unit (1 for mg, 2 for kg, 3 for ton): 4
Invalid unit
*User input is shown in bold font

Page 2
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Advanced Selection

6. Engy went to a restaurant and ordered a soup and a meal.


Restaurant menu contains: soups: vegetables(v), seafood, mushroom(v)
meals: burger, grilled chicken, mashed potatoes(v).
[ v stands for vegetables soup or meal]
If Engy had her all order vegetables so she loves vegetables, if it had no vegetables
at all so she hates vegetables otherwise she neither hate nor love it.
Sample Run 1* Sample Run 2*

Please pick a soup: mushroom Please pick a soup: sea food


Please pick a meal: mashed potatoes Please pick a meal: grilled chicken
You love vegetables You hate vegetables
*User input is shown in bold font

7. Write a program that reads the coefficients (a, b, c) of the quadratic equation ax2 +
bx + c = 0. The program computes the roots (x1 and x2) if they exist. The roots of the
equations can be one of the following four cases:
● any x is a solution (if a = b = c = 0)
● no solution (if a = b = 0, c ~= 0)
● one real root (if a = 0, so the root is −c/b)
● two real or complex roots (if not any of the above cases)
−𝑏 ± √𝑏 2 − 4𝑎𝑐
𝑥1,2 =
2𝑎

Input Output
[0 0 0] Any x is a solution
[0 0 2] No solution
[0 1 3] -3
[1 -5 6] 2
3

Page 3
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Advanced Selection

8. Ahmed invited his friends to a party. He received many presents. If anyone brought
him something in his mind he will be over the moon and will send that friend a
message to thank him.

Sample Run 1:
Enter friends:Mark,Hossam,Majed,Ali,Nader
Enter presents:car, playstation, football, basketball shoes
what is in his mind? basketball shoes
Oh nader, Thank you friend :)

Sample Run 2:
Enter friends:Mark,Hossam,Majed,Ali,Nader
Enter presents:car, playstation, football, basketball shoes
what is in his mind? apple watch
oops, Sorry none.
*User input is shown in bold font

Page 4
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Advanced Selection

9. [Advanced - Extra] Modify problem 5 so that it can be executed up to 5 times or


until the user decides to end program. After each execution ask the user if he
wishes to make another conversion or not. [See sample runs below]

Sample runs*
Sample Run 1 Enter weight: 10
Enter weight unit (1 for mg, 2 for kg, 3 for ton): 2
Converting kg to gram
10 kg = 10000 gram
Do you want to make another conversion? Yes
Sample Run 2 Enter weight: -5
Invalid weight
Do you want to make another conversion? Yes
Sample Run 3 Enter weight: 5
Enter weight unit (1 for mg, 2 for kg, 3 for ton): 4
Invalid unit
Do you want to make another conversion? No
Good Bye …
*User input is shown in bold font

Page 5

You might also like