In: Computer Science
python
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center> <h1>CpE 4040 Midterm-2 Exam</h1>
</center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1- Write a Python code which asks two numbers from the user,
calculates the sum and print the result. Create a function to
calculate the summation and return the result
value.<br>\n",
"i.e. <br>\n",
"First Number:12 <br>\n",
"Second Number:23 <br>\n",
"12 + 23 = 35"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2- Create 20 random integers in an array which has the numbers
between 10 and 100 (includes 10 and 100). Sort and print the
numbers in descending order."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3- Write a Python code that asks a string from the user and prints
only the lower case letters in that string."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4- Create an evenly spaced 21-element array between 2.2 and 12.4.
Find the median of the array. Print both the array and median
value. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5- Write a simple number guessing game Python program. In this
code, generate a random integer between 1-10 (both included) and
ask the user to guess the number in three attempts. \n",
"Print appropriate responses to the user such as \"You won!\" or
\"You failed\" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6- Generate an array of numbers from 1 to 15. Reshape it to make
3x5 matrix. Convert the matrix into data frame and print the even
number of columns. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1).
Code Screenshot:
Code:
#function received num1 and num2
def summation(num1,num2):
#adds num1, num2 and store in value
variable
value= num1+num2
#return value
return value
#take input from user, convert it to int and store in num1
num1=int(input("First Number: "))
#take input from user, convert it to int and store in num2
num2=int(input("Second Number: "))
#call summation function, pass num1,num2 and store the returned
value un result
result=summation(num1,num2)
#display to user
print(num1,"+",num2,"=",result)
Output:
2).
Code Screenshot:
Code:
#import random for generating random number
import random
#declare array
randomArray=[]
#run for loop 20 times(from 0 to 19)
for i in range(20):
#generate random number using randint and append
it to randomArray
randomArray.append(random.randint(10,100))
#sort randomArray in reverse order i.e. descending order
randomArray.sort(reverse=True)
#print all the elements of randomArray
for i in randomArray:
print(i)
Output:
3).
Code Screenshot:
Code:
#take input from user
text=input("Enter string: ")
#declare a variable
loweredString=""
#traverse all the letters in text
for i in text:
#if the letter is in lower case
if(i.islower()):
#add the i letter to
variable loweredString
loweredString+=i
#display the lower case letters
print("Lowered case letters are: "+loweredString)
Output:
4).
Code Screenshot:
Code:
#import numpy module as np
import numpy as np
#create array numbers using np.linspace whose element
value
#will start at 2.2, end at 12.4 and will contain 21 element
numbers=np.linspace(2.2,12.4,21)
#find median of array numbers using np.median and store in
numbersMedian
numbersMedian=np.median(numbers)
#display array and median value
print("Array:" )
print(numbers)
print("Median:",numbersMedian)
Output:
5).
Code Screenshot:
Code:
#import random for generating random numbers
import random
#generate random number between 1 and 10 and store in
number
number=random.randint(1,10)
#variable for checking whether the user guessed correct
number
success=False
#variable for no of tries
trial=0
#run while loop till user have not made 3 guesses
while(trial!=3):
#take input from user
guessedNumber=int(input("Guess a number between
1-10: "))
#if the number guessed by user is not same as
the number generated
if(guessedNumber!=number):
#increment trial
value
trial+=1
#else
else:
#set success to
True
success=True
#move out of while
loop
break
#when user guessed the correct number
if(success==True):
print("You guessed the correct
number",number)
print("You won!!!")
#when user did not guess the correct number
else:
print("The correct number is ",number)
print("You Failed!!!")
Output:
Sample1:
Sample 2:
6).
Code Screenshot:
Code:
#import numpy and panda
import numpy as np
import pandas as pd
#create an empyt list
numbers=[]
#from 0 to 14
for i in range(15):
#append i to numbers after adding 1
numbers.append(i+1)
#reshape the list to a matix of colims 5
matrix=np.reshape(numbers,(-1,5))
#convert the matrix to data frame
matrix=pd.DataFrame(matrix)
#for the columns in matrix
for col in matrix:
#if it is a even columns
if(col%2==0):
#display the
column
print (matrix[col])
Screenshot: