You are on page 1of 5

Assignment 4

Group No: 1
Programming for Big data

Name Student ID
Rajan Vireshbhai Thakkar 8881494
Hareesh Varatharaj 8903862
Sujay Silas Macwan 8878150
Krima Mahendrakumar Prajapati 8888662
Question 1:
Create a program that determines and displays the number of unique characters in a string
entered by the user. For example, Hello, World! has 10 unique characters, while zzz has
only one unique character.
* Use a dictionary or set to solve this problem.
* Define a function
Program:
def unique(string):

str = set(string)

length = len(str)

return length

print("This program checks the number of unique characters in a string..")

string = input("Enter the string:\n")

length_of_string = unique(string)

print("The number of unique characters in your string


is:",length_of_string)

Brief :
● A string is taken as input from the user and the string is then passed to a function
unique.
● The unique(arg) is a parameterized function which takes the string inputted by the
user as the parameter and then returns a value.
● Inside the unique() function, the user string is pushed into a set(). A set in python can
only store distinct values. So even though duplicate values are added, only one
instance will be retained among the duplicates. The unique() function then returns the
length of the set.
● The return value from the unique() will be the number of unique characters in the
string the user enters and is printed.
Output:

Question 2:
This exercise examines the process of identifying the maximum value in a collection of
integers. Each of the integers will be randomly selected from the numbers between 1 and
100. The collection of integers may contain duplicate values, and some of the integers
between 1 and 100 may not be present. * Use randrange and import the relative python
library to generate the random numbers
Program
# Import random function to use randrange

import random

def randnum():

i=0

x=[]

# while loop until i is not equal to 10

while i<10:

b = random.randrange(1,101)

x.append(b)

i+=1

return x

list = randnum()

print("Random number from 1 to 101 : ",list)

Brief :
● The randnum function returns a 10 random number.
● Initial counter (i) to 0 while loop will run until i is less then 10
● random.randrange(1, 101) generates a random number between 1 and 101
(exclusive) that means 100. And this store into b
● Created x as list and append all the value in x and return the list
● At last we print the list.
Question 3
In the game of Scrabble™, each letter has points associated with it. The total score of a
word is the sum of the scores of its letters. More common letters are worth fewer points while
less common letters are worth more points.
The points associated with each letter are shown below:
One pointA, E, I, L, N, O, R, S, T and U , Two points D and G, Three points B , C, M and P
Four points F, H, V, W and Y, Five points K, Eight pointsJ and X, Ten points Q and Z
Write a program that computes and displays the Scrabble™ score for a word.
Create a dictionary that maps from letters to point values.
Then use the dictionary to compute the score.
* Use a function

Program
#define function for calculating word

def scrabbleTM(word):

# dictionary that pair the latter and value

l = {'A':1, 'B':3, 'C':3, 'D':2, 'E':1, 'F':4, 'G':2, 'H':4, 'I':1,


'J':8,

'K':5, 'L':1, 'M':3, 'N':1, 'O':1, 'P':3, 'Q':10, 'R':1, 'S':1,


'T':1,

'U':1, 'V':4, 'W':4, 'X':8, 'Y':4, 'Z':10}

# calculate the score

score = 0

for letter in word.upper():

score += l.get(letter, 0)

return score

Brief :
● scrabbleTM function defines a dictionary and each letter to its point value.
● Then for loop through each letter in the input word, converts it to uppercase which
upper() function will do because dict have all capitalize value,
● get() method help to find value. If the letter is not in the dictionary, it is assigned a
value of 0 points.
● At last, we add the the point values for each letter in the word to score and returns
the score.
Output

You might also like