My Assignment Help

BIM114 Programming Fundamentals: Python Mix Assessment Answer

Bachelor of Interactive Media BIM114- Programming Fundamentals

Assignment

All the problems listed below must be solved by developing a structure chart, writing pseudocode and by writing program structure (using comments). Each problem is worth 10 marks. Documentation is worth 4 marks and the program code 6 marks.

1.

2. Write a program to read a text file, employeenames.txt, and creates anther file called

“usernames.txt” with usernames with format first character of first word and the complete

second word. Your program must work with any given two files.

Ex: the first name in the employeeNames.txt file is Sai Lakkaraju. His username must be

“slakkaraju”

3.

4.

5.

6. The body mass index (BMI) is calculated as a person’s weight (KG) divided by the square of the person’s height (in inches). A BMI in the range 19-25 (inclusive) is considered healthy. Write a program that calculates a person’s BMI and prints a message telling whether they are above, within, or below the healthy range.This program must keep on calculating BMI for the given

input till user says, “they don’t want to continue”.

7.

8. Hint: use a while loop and ask the user if they want to calculate BMI. If the answer is “Yes”, continue with calculations. If the answer is “No” quit the program.

9.

10. Example:

Input:

Height = 1.78 m Weight = 102 KG Process:

11.

12. BMI = 102 / (1.78*1.78) = 32.2

Output:

Your BMI is 32.19. You are obese. Lose weight to live longer.

13.

14.

15.

16.  Write a program to compute the “N”th Fibonacci number where “N” is a value entered by the

user. The Fibonacci sequence starts with 1,1,2,3,5,8, 13Each number in the sequence is the sum of the previous two (except the first two 1,1).

Ex: input n = 10

17.

18.

out put

10th Fibonacci number is 55.

The Fibonacci sequence is 1,1,2,3,5,8,13,21,34,55.

4. Write a program that will read a text file as input and will display the word frequency in the text file.

Ex: if the input text file contains the following text; “Hello, my name is Sai Kiran Lakkaraju. I am a staff at

Win college. I started working at Win college this term; term 2, 2020.”

Output should look like:

Answer

1. Write a program to read a text file, employeenames.txt, and creates anther file called “usernames.txt” with usernames with format first character of first word and the complete second word. Your program must work with any given two files.

Ex: the first name in the employeeNames.txt file is Sai Lakkaraju. His username must be

“slakkaraju”

Program Pseudocode
1. get input file name from the user

2. read the input file line by line

3. get first name from the read line

4. get last name from the read line

5. create the username

6. write the username into the file

7. close the files.

Program structure

#get input file name from the user

# open the input file in read mode 

# open the output file in write mode

#read the input file line by line

    # get first name from the line

    #get last name from the tline

    #create the username

    #save the username to the file

#close the files

Program Code

#get input file name from the user

filename = input("Enter input file name: ")

# open the input file in read mode

file1 = open(filename,"r") 

# open the output file in write mode

outFile = open("usernames.txt","w+") 

#read the input file line by line

for line in file1:

    line = line.strip()

    # get first name from the line

    firstName = line.split(" ")[0]

    #get last name from the tline

    lastName = line.split(" ")[1]

    #create the username

    username = firstName.lower()[0:1] + lastName.lower()

    #save the username to the file

    outFile.write(username+"\n")    

print("Usernames saved to 'usernames.txt'")

#close the files

file1.close()

outFile.close()

2. The body mass index (BMI) is calculated as a person’s weight (KG) divided by the square of the person’s height (in inches). A BMI in the range 19-25 (inclusive) is considered healthy. Write a program that calculates a person’s BMI and prints a message telling whether they are above, within, or below the healthy range.This program must keep on calculating BMI for the given input till user says, “they don’t want to continue”.

Program Pseudocode

1. loop till user want to calculate BMI

2. get height from the user

3. get weight from the user

4. calculate bmi

5. print BMI and the user message according to BMI

6. Check whether the user wants to continue or not

Program structure

#loop till user choice is yes

    # get user height

    # get user weight

    #calculate BMI

    #print the BMI

    # BMI is less than 18.5, underweight

    # BMI is greater than 24.9, overweight

    # check whether the user wants to continue?

Program Code

choice = 'yes'

#loop till user choice is yes

while choice =='yes':

    # get user height

    height = float(input("Height(M): "))

    # get user weight

    weight = float(input("Weight(kg): "))

    #calculate BMI

    bmi = weight / (height * height)

    #print the BMI

    print("Your BMI is "+str(bmi)+".", end = "")

    # BMI is less than 18.5, underweight

    if bmi < 18.5:

        print("You are underweight. Gain Weight to live longer.")

    # BMI is greater than 24.9, overweight

    elif bmi >24.9:

        print("You are overweight. Lose Weight to live longer.")

    else:

        print("Congrats your BMI is NORMAL.")

    # check whether the user wants to continue?

    choice = input("\nDo you want to calculate other BMI? ").lower()

3. Write a program to compute the “N” th Fibonacci number where “N” is a value entered by the user. The Fibonacci sequence starts with 1,1,2,3,5,8, 13.... Each number in the sequence is the sum of the previous two (except the first two 1,1). Ex: input n = 10

Program Pseudocode

1. get the value of n from the user

2. initialize i, j and a list of numbers

3. add i and j to the list of numbers

4. find n-2 fibonaccin numbers and add them to the list of numbers.

5. display the result

Program structure

# get n as input 

# list to contain the fibonacci numbers

# add iitial i and j to list

# add fibonacci n-2 fibonacci numbers

    # add the current number

# print the result    

Program Code
# get n as input 

n = int(input("Enter the value of n : "))

# list to contain the fibonacci numbers

numbers = []

i = 1

j = 1

# add iitial i and j to list

numbers.append(i)

numbers.append(j)

# add fibonacci n-2 fibonacci numbers

for ii in range(0, n-2):

    sum = i + j

    j = i

    i = sum

    # add the current number

    numbers.append(i)

# print the result    

print(str(n)+"th Fibonacci number is "+str(i)+".")

print("The Fibonacci sequence is "+ str(numbers)[1:len(str(numbers))-1]+".")

4. Write a program that will read a text file as input and will display the word frequency in the text file.

Program Pseudocode

1. get input file name from the user

2. read the input file line by line

3. create a string which contains all the punctuation symbols

4. remove all the pucnctuations from the line

5. for each word in the line, add its frequency to the dictionary

6. sort the final dictionary by count and the word and print the result

Program structure

#get input file name from the user

# open the input file in read mode

# string with all punctuations

#create a new empty dictionary

#read the input file line by line

        # this character is not in punctuations

            # add to string if this character is not a punctuation mark

    # split by spaces

    # go through each word

            # if word is in dictionary add count by 1

            # if word not in dictionary, set count as 1

#sort and print the result

Program Code

#get input file name from the user

filename = input("Enter input file name: ")

# open the input file in read mode

file1 = open(filename,"r") 

# string with all punctuations

punctuations = '''1234567890!()-[]{};:'"\,<>./[email protected]#$%^&*_~'''

#create a new empty dictionary

freq = {}

#read the input file line by line

for line in file1:

    line = line.strip()

    nLine = ''

    for c in line:

        # this character is not in punctuations

        if c not in punctuations:

            # add to string if this character is not a punctuation mark

            nLine = nLine + c

    # split by spaces

    words = nLine.split(" ")

    # go through each word

    for word in words:

        word = word.lower()

        if len(word) > 0:

            # if word is in dictionary add count by 1

            if word in freq:

                freq[word] +=1

            # if word not in dictionary, set count as 1

            else:

                freq[word] =1

#sort and print the result

for x in sorted(freq.items(), key=lambda x: (x[1],x[0]), reverse=True):

    print(x[1],x[0])

Customer Testimonials