Computer Science

Here we provide examples of solutions and price/time deadlines of students' work that we have solved in the past. If while looking at these examples you find yourself wishing soeone could have done that for you? then go ahead and place an order now. We can't wait to help you make your college experience stress free and fun! Place an Order

Subject: Introduction to Python. Type: Homework. Due date: 1 day. Price: $3.95

General remarks:

  • this was written in Python 3 (syntax would differ for Python 2)
  • to print out your results in IDLE hit fn+F5

Problem 1:   Write the decimal equivalent of the fraction

Solution:

First we can print out the solution we are working on. To do that we use command print followed by a statement

in quotation marks. When we put characters in quotation marks, we call those strings; they are like statements.
print(“Solution 1:”)

Next we want to think of how we can write the decimal form of the fractions from ½, 1/3 , ¼, 1/5, …. 1/10. We want to create a loop that will iterate over those value and print out the decimal format, so we know we will need to use some type of looping mechanism and we will need to write our numerator as a decimal to signal to Python to return a decimal. Two solutions are provided: in the first we designate a variable x as 2 and set the condition that as long as x is less than 11 (non-inclusive) then we enter the loop. If the while condition is true then we print the fraction in decimal form (1.0/x) and we increment the value of x so we can print all the fractions with denominators 2 to 10. The other approach would be to have all of our denominators already created via the function range(2,11) where we iterate over the list of values in the range 2 to 11 via “for i in range(2,11)” and then print the fraction as a decimal by inputting i for the denominator. Both solutions are correct, but the second way is cleaner and would run much quicker.

Code:

###
print("Solution 1:")
x = 2
while x < 11:
   print(1.0/x)
   x += 1
for i in range (2,11):
   print(1.0/i)
###

Problem 2: Get a user input and return a print out of a countdown from that number

Solution:

Once again we want to signal that that we are now working on Solution 2 so we print(“Solution 2:”).

Next we want to signal the user to input a value and print out a countdown from that number. To create a user input, we use the function “input()” which takes in a String that you want to be printed out telling the user what to do. Thus we write: input(“Enter a number: “). As before we are using a String as text in quotations as a message. We want to set this equal to a variable name so that we can reuse it in further calculations, so we set it equal to x, thereby naming it x. Next, we turn this inputted value into an integer so that we can countdown by 1s; for example if someone enters 4.3 it would reset the value that x equals to 4.

Now we want to approach the problem of creating a countdown. We know that the possible inputs are integers, that is positive and negative values, so we need to take into account both of those cases. We use the “while-loop” because we want to go through a range of values and print them out recursively. We also want to print out zero so we include it in our boundaries for x. So our two conditions are either when x is less than or equal to zero, in which case we want to print out that value and increment up by one to reach zero, otherwise we want to increment down. This way, we will print each value and then the value next closest to zero and so on, thereby creating a countdown.

Code:

###
print("Solution 2:")
x = input("Enter a number: ")
x = int(x)
while x >= 0:
  print (x)
   x = x-1
while x <= 0:
   print (x)
   x = x+1
###

Problem 3: Get an input value that is divisible by 2 and continue until successful

Solution:

Once again we want to signal that that we are now working on Solution 2 so we print(“Solution 3:”).

Next we want to signal the user to input a value and print out a countdown from that number. To create a user input, we use the function “input()” which takes in a String that you want to be printed out telling the user what to do. Thus we write: input(“Enter a number divisible by 2: “). As before we are using a String as text in quotations as a message. We want to set this equal to a variable name so that we can reuse it in further calculations, so we set it equal to y, thereby naming it y.

Next we want to check if the user correctly inputted a number divisible by 2. To do that we check that the remainder of  the value divided by 2 is 0 by using % instead of a / to indicate remainder. So if that condition holds, then we want to print the statement "Congratulations, you picked a correct number!". If they picked an incorrect value, else, we say “Try again!”.  Now if we want the program to then prompt the person to “Enter a number divisible by 2:” we need to add another condition that tells it to reenter that loop. In order to do that we place a while loop outside of the two conditional loops we already have.  So suppose we have a variable a that we set to True, so long as a is True (while a:), we want to reenter this loop. Now, without doing any additional changes we will enter an infinite loop because a is indefinitely set as true, so we want to tell the program when to stop and we want it to stop once the person correctly chooses a number divisible by two. So we will set a as False ( a=False) after we print the congratulations statement.

Code:

###
print("Solution 3:")
a = True
while a:
    y = int(input("Enter a number divisible by 2: "))
    if y % 2 == 0:
        print("Congratulations, you picked a correct number!")
        a = False
    else:
        print ("Try again!")
###

Problem 4: Get an input, print # of vowels, print the string without vowels and a list of unique consonants

Solution:

Once again we want to signal that that we are now working on Solution 2 so we print (“Solution 4:”).

Next we want to signal the user to input a value and print out a countdown from that number. To create a user input, we use the function “input()” which takes in a String that you want to be printed out telling the user what to do. Thus we write: input(“Enter a word: “). As before we are using a String as text in quotations as a message. We want to set this equal to a variable name so that we can reuse it in further calculations, so we set it equal to “word”, thereby naming it “word”. In this case we want to identify or cast their value as a String so that we can easily make edit (delete, insert, recompile) the partitions.

Next we define the “values” that we consider vowels in a list so that we can access them and iterate over them when we compare it to our String. Another key thing to consider is when Python compares our defined vowels to a letter in the inputted word, it won’t equate a lowercase vowel with an uppercase vowel, so we need to convert our word to all lowercase (word.lower()).
Our first task is to count how many vowels are in the word, we do so by once again iterating over each letter in the word and there is a built in iteration in Python allowing us to say for i (each letter) in vowel, we want to see if that letter is one of the letters in our list of vowels, we want to increase our count by 1. So before we want to set out count to 0 as a variable. Now we can just print the number of vowels and we can label them as vowels using the String “vowels”. You can print a variety of objects by separating them with a comma.

Next we want to remove the vowels in the word and print a list of consonants. Let’s first think of what we have: we have a word named “word” and we have our vowels stored in a list named “vowels”. Now we can go through each of those vowels in the list (for vowel in vowels), and if we see that vowel in the word we want to remove it. One way of doing that is replacing that letter with “nothing”, that is “”, which is a string without a space. So we use the function replace and input what we are replacing and with what. Now we want to save this newly edited word as word because then we can use the same variable name for the word without having to rename everything and we can maintain the edits we are using, so that the variable name “word” stores the most recently edited version of the original word. Finally we just print the word, once we have gone through all of the vowels in the list.

Our final step is to create a list of the unique consonants in the word. First we want to create a new list, which we named spaces (spaces = []). Then we want to iterate over each letter in the word, and if the consonant isn’t already in the list (since we want the unique consonants), then we want to add that letter to the list. Finally we print the list of unique consonants.

Code:

###
print("Solution 4:")
word = str(input("Enter a word: "))
vowels = ["a", "e", "i", "o", "u"]
new_word = word.lower()
num_vowels = 0
for i in new_word:
    if i in vowels:
        num_vowels = num_vowels+1
print (num_vowels, "vowels")
for vowel in vowels:
    word = word.replace(vowel, "")
print (word)
spaces = []
for i in word:
    if not i in spaces:
        spaces.append(i)
print (spaces)