CSCI1105

View the Project on GitHub

Assignment 2 Question 1

Instructions

You will be given a sentence. You must break this sentence into a list of words, then output the length of the sentence, the length of the list, the sentence in reverse order by character, and the sentence in reverse order by word.

Details

Input You will be given one line of input:

Processing

Break the sentence into a list using the string split() method. You can learn more about split() here: https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split

Output

Print the following information on separate lines as shown in the examples below:

Example

Sample Input

“The rain in Spain stays mainly on the plain.”

Sample Output

44

9

.nialp eht no ylniam syats niapS ni niar ehT plain. the on mainly stays Spain in rain The

Sample Input

“You will be given a sentence.”

Sample Output

29

6

.ecnetnes a nevig eb lliw uoY sentence. a given be will You

Sample Input

“To be, or not to be, that is the question.”

Sample Output

42

10

.noitseuq eht si taht ,eb ot ton ro ,eb oT question. the is that be, to not or be, To

My Solution also hosted here


# Got the input
sentence = input()

# Got the length of the input and printed immediately
print(len(sentence))

# split the input then got the length of the splitted input
splitted = sentence.split()
print(len(splitted))

# used list function to split the input character by character
doublesplitted = list(sentence)

# created a new empty list to add the splitted list in reverse order
newone = []
for i in range(len(doublesplitted)):
    newone.append(doublesplitted[-(i+1)])

# printed it except the last one    
for i in range(len(doublesplitted)-1):
    print(newone[i], end="")

# printed the last one here because of above end="" results in having 
# future stuff printed on the same line
print(newone[-1])

# created an empty list for adding the splitted input in reverse order
new = []
for i in range(len(splitted)):
    new.append(splitted[-(i+1)])

# then printed it
for i in range(len(splitted)):
    print(new[i], end=" ")

Question 2

Instructions

You are going to convert Roman numbers to modern Hindu-Arabic numbers. The ancient Romans used the following letters to represent numbers:

Roman Number Hindu-Arabic Number I 1 V 5
X 10 L 50 C 100 D 500 M 1000 These letters were combined to represent numbers using an additive notation. The larger portions of the number were written first. For example, XXIII is 23 (10 + 10 + 1 + 1 + 1) or DCXI is 611 (500 + 100 + 10 + 1).

An odd part of this system is some numbers used a subtractive notation. The easy examples to understand are IV is 4 (1 before 5 or -1 + 5) and IX is 9 (1 before 10 or -1 + 10). The same sort of things happens from 40 (XL), 90 (XC), 400 (CD), 900 (CM).

The easiest way to understand the subtractive notation is if a letter represents a number that is less than that represented by the letter to its right, the left number is subtracted from the righthand number.

You can read more about Roman numbers here: https://en.wikipedia.org/wiki/Roman_numerals#Use_of_additive_notation

Details

Input

You will be given receive one line of input:

Processing

Output

Sample input/output:

Input

XVIII

Output

Roman Number: XVIII

Hindu-Arabic: 18

Input

LXXIV

Output

Roman Number: LXXIV

Hindu-Arabic: 74

Input

CCLXXV

Output

Roman Number: CCLXXV

Hindu-Arabic: 275

Input

CDXCIX

Output

Roman Number: CDXCIX

Hindu-Arabic: 499

Input

CMXLIX

Output

Roman Number: CMXLIX

Hindu-Arabic: 949

My Solution also hosted here


# Input
romanNumber = input()

# splitted the input by character by character
splitted = list(romanNumber)

# converted the roman numeral to standard number
for i in range(len(splitted)):
    if splitted[i] == "I":
        splitted[i] = 1
    elif splitted[i] == "V":
        splitted[i] = 5
    elif splitted[i] == "X":
        splitted[i] = 10
    elif splitted[i] == "L":
        splitted[i] = 50
    elif splitted[i] == "C":
        splitted[i] = 100
    elif splitted[i] == "D":
        splitted[i] = 500
    elif splitted[i] == "M":
        splitted[i] = 1000

# This part was not necessary but to make sure
# I made a for loop to combine the ones that make up two or three 
for i in range(len(splitted)-2):
    if splitted[i] == 1 and splitted[i+1] == 1 and splitted[i+2] == 1:
        splitted[i] = 3
        splitted[i+1] = 0
        splitted[i+2] = 0
    elif splitted[i] == 1 and splitted[i+1] == 1 and splitted[i+2] != 1:
        splitted[i] = 2
        splitted[i+1] = 0

# I made a for loop for IV, IX and others where I, X and C is used to mean 
# one, ten and hundred less than of something
for i in range(len(splitted)-1):
    if splitted[i] == 1 and splitted[i+1] == 5:
        splitted[i] = 4
        splitted[i+1] = 0
    elif splitted[i] == 1 and splitted[i+1] == 10:
        splitted[i] = 9
        splitted[i+1] = 0
    elif splitted[i] == 10 and splitted[i+1] == 50:
        splitted[i] = 40
        splitted[i+1] = 0
    elif splitted[i] == 10 and splitted[i+1] == 100:
        splitted[i] = 90
        splitted[i+1] = 0
    elif splitted[i] == 100 and splitted[i+1] == 500:
        splitted[i] = 400
        splitted[i+1] = 0
    elif splitted[i] == 100 and splitted[i+1] == 1000:
        splitted[i] = 900
        splitted[i+1] = 0
    
# made a sum variable for the for loops that add the corrected numbers 
# in the list
summ = 0
for i in range(len(splitted)):
    summ += splitted[i]





# Output
print("Roman Number:", romanNumber)
print("Hindu-Arabic:", summ)

Question 3

Instructions

Last assignment, you took up playing “21” (otherwise known as “Blackjack”) and only had to worry about two cards.

This time, you will start will be given a sequence of ten cards. You must work your way through the cards and report where you would stop. You will stop if the current card brings your card total to 16 or more. It the total is between 16 and 21, you will say “Hold!”; if the card total goes over 21, you will say “Bust!”

Details

Input

The program will start with one line of input:

Processing

Just like last time, the cards have an integer value based on the following:

Create a list from tenCards

Work your way through the list, one card at a time: [HINT—This is a loop]

  1. Calculate your card total (that is, add up values of the cards you’ve looked at so far—if you are looking at the first card, the card total is just that card; if you are looking at the first two cards, total those two; etc.)
  2. If the card total is 16 or better, end the loop.
  3. If the card total is less than 16, look at the next card in the list.

Output

Print the following on separate lines as shown in the examples below:

Example

Sample Input

“A A 3 2 Q 2 10 2 3 4”

Sample Output

[‘A’, ‘A’, ‘3’, ‘2’, ‘Q’]

17

Hold!

Sample Input

“5 5 5 10 10 10 A A A A”

Sample Output

[‘5’, ‘5’, ‘5’, ‘10’]

25

Bust!

Sample Input

“J A 3 2 Q 2 10 2 3 4”

Sample Output

[‘J’, ‘A’, ‘3’, ‘2’]

16

Hold!

Sample Input

“2 2 2 2 3 3 3 3 Q A”

Sample Output

[‘2’, ‘2’, ‘2’, ‘2’, ‘3’, ‘3’, ‘3’]

17

Hold!

My Solution also hosted here

# Input
tencards = input()

# made two variables which split the same input
# but one just to print and the other for conversion and calculation
newList = tencards.split()
newListt = tencards.split()

# made a sum variable to be used for while loop to stop when the sum 
# goes over 16
sum = 0
numm = 0
while sum < 16:
    if newList[numm] == "A":
        newList[numm] = 1
    elif newList[numm] == "J":
        newList[numm] = 10
    elif newList[numm] == "K":
        newList[numm] = 10
    elif newList[numm] == "Q":
        newList[numm] = 10
    sum = sum + int(newList[numm])
    numm += 1

# printed the input variable from the start and ended where it goes 
# right after above 16
print(newListt[0:numm])   

# prints the sum after it goes above 16
print(sum)

# made an if statement for when it's still below 22 or else
if sum < 22:
    print("Hold!")
else:
    print("Bust!")