Python Intermediate - Withoutcode
Python Intermediate - Withoutcode
ipynb - Colaboratory
Python-Intermediate
Here you can start digging into our intermediate-level programming questions that will teach you new Python concepts. This category is for
intermediate Python developers who already know the basics of Python development and want to expand their knowledge.
Becoming a Python expert takes time, but over time you’ll master this beautiful programming language. It’s worth it!
#code here
2. Good! Shall we try solving above problem in shhorter way using list comprehention? It will be amazing. Here we go!
#code here
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 1/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
3. define a funtion 'occurence' which takes 2 inputs, one list 'lst' and another one a number 'n' and count the number of occurrences of n in
your list lst.
#your code
number = 3
#code here
5. Reverse a given integer number num = 47594038222 and display the count of total number of digits in your revered number using while
loop.
#code here
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 2/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
6. Can you reverse you given num without using loop in just one line?
num = 47594038222
22283049574
7. Write a function that checks whether a given sequence of number is IP address or not and print all valid possible IP from a given digit. If
not an IP then it prints empty list
Note: According to Wikipedia, IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers,
each ranging from 0 to 255, separated by dots, e.g., 172.16.254.1
#Code here
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 3/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
['255.255.11.135', '255.255.111.35']
[]
#code here
s="Wow!!!, we are going ahead with our course to learn machine learning ---are'nt you excited? :)"
# code here
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 4/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
Wow we are going ahead with our course to learn machine learning arent you excited
10. Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column
of the array should be i*j.
digit1=3
digit2=4
#code here
11. For given string S and integers P and Q, which denotes the cost of removal of substrings “he” and “lo” respectively from S, the task is to
find the maximum cost of removing all occurrences of substrings “he” and “lo”.
# code here
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 5/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
S = "hellohellohellohellohello"
# Costs
P = 6;
Q = 4;
print(MaxCollection(S, P, Q));
50
12. Given a binary string S , the task is to find the smallest string possible by removing all occurrences of substrings “01” and “11”. After
removal of any substring, concatenate the remaining parts of the string.
Example
Input: S = “0010110”
Output: String = 0
#code here
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 6/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
s="0010110"
13. Write a function such that it accepts two integers n, m as arguments Find the sum of all numbers in range from 1 to m(both inclusive)
that are not divisible by n. Return difference between sum of integers not divisible by n with sum of numbers divisible by n
Example
Input
n:4
m:20
Output
90
#code here
4
20
90
14. Write a function count_code that return the number of times that the string "code" appears anywhere in the given string, except we'll
accept any letter for the 'd', so "cope" and "cooe" count.
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 7/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
count_code('aaacodebbb') → 1
count_code('codexxcode') → 2
count_code('cozexxcope') → 2
#code here
1
2
2
#code here
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 8/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
#code here
Number of vowels: 5
17. well done. Now we want you to solve above question in just one line. have faith in your, you can do it.
Number of vowels: 5
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 9/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should
print
Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl',
then your program should print
In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print
#code here
Awsesome! Great job so far. Hope you fell in love with python now.
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 10/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
But wait we want to give a treat..:) We have a surprise for you in form of one of the task, in this task you have to create a game in python. You
can take up this task as a mini project of your's in python. Hmm.. Sounds good. Isn't it?
So lets gets started.. we have given the instructions below for the task in detail
Hangman Game
In this project we are going to create Hangman Game using python
Q. What is the hangman game?
A:- Hangman is a guessing game. The player will tries to guess a word picked by the computer, by suggesting letters within a certain number of
guesses
Task 1
Create a function is_word_guessed that takes in two arguments :-
secret_word, letters_guessed
returns True if all of the letter of secret_word are in letters_guessed, returns False if not
Assumptions
secret_word: string, the word the user is guessing; assumes all letters are lowercase
letters_guessed: list (of letters), which letters have been guessed so far; assumes that all letters are lowercase
Example:-
secret_word = 'apple'
letters_guessed = ['e', 'a', 'k', 'p', 'l', 's']
is_word_guessed() will return True.
secret_word = 'apple'
letters_guessed = ['e', 'i', 'k', 'p', 'r', 's']
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 11/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
# Code here
Task 2
Create a function get_guessed_word that takes in two arguments :-
secret_word, letters_guessed
returns string, comprised of known letters, and unknown letters represented by an underscore and a space (_ )
Assumptions
secret_word: string, the word the user is guessing;
letters_guessed: list (of letters), which letters have been guessed so far
Example:-
secret_word = 'apple'
letters_guessed = ['e', 'i', 'k', 'p', 'r', 's']
get_guessed_word() will return '_ pp_ e'
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 12/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
# code here
Task 3
Create a function get_available_letter that takes in one arguments
letters_guessed
returns string (of letters), comprised of letters that represents which letters have not yet been guessed.
Assumptions
letters_guessed: list (of letters), which letters have been guessed so far;
Example:-
letters_guessed = [’a’, ’l’, ’m’]
get_available_letter() will return 'bcdefghijknopqrstuvwxyz'
import string
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 13/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
print(string.ascii_lowercase)
output:- abcdefghijklmnopqrstuvwxyz
def get_available_letters(letters_guessed):
'''
letters_guessed: list (of letters), which letters have been guessed so far
returns: string (of letters), comprised of letters that represents which letters have not
yet been guessed.
'''
# FILL IN YOUR CODE HERE
Great, We have created all of the Helper functions, now let's start our work on the actual Hangman Game
Hangman Game
Create a function hangman that takes in one argument
secret_word :- string, the secret word to guess.
At the start of the game, let the user know how many letters the secret_word contains and how many guesses s/he starts with.
Before each round, you should display to the user how many guesses s/he has left and the letters that the user has not yet guessed.
Ask the user to supply one guess per round. Remember to make sure that the user puts in a letter!
The user should receive feedback immediately after each guess about whether their guess appears in the computer's word.
After each guess, you should display to the user the partially guessed word so far.
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 14/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
If the user inputs a letter that hasn’t been guessed before and the letter is in the secret word, the user loses no guesses.
Consonants: If the user inputs a consonant that hasn’t been guessed and the consonant is not in the secret word, the user loses one
guess if it’s a consonant.
Vowels: If the vowel hasn’t been guessed and the vowel is not in the secret word, the user loses two guesses. Vowels are a, e, i, o, and u. y
does not count as a vowel.
The game should end when the user constructs the full word or runs out of guesses.
If the player runs out of guesses before completing the word, tell them they lost and reveal the word to the user when the game ends.
If the user wins, print a congratulatory message and tell the user their score.
The total score is the number of guesses_remaining once the user has guessed the secret_word times the number of unique letters in
secret_word.
Demo Game 1
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 15/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
Demo Game 2
Hints
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 17/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
def no_of_unique_words(string):
#code here
def hangman(secret_word):
#code here
# When you've completed your hangman function, test it out to see it is working fine
# (hint: you might want to pick your own
# secret_word while you're doing your own testing by changing 'apple' to any other string)
hangman('apple')
Good guess: a_ _ _ _
-----------------------------------
You have 6 guesses left.
Available letters: bcdefghijklmnopqrstuvwxyz
Please guess a letter: a
You've already guessed that letter
-----------------------------------
You have 6 guesses left.
Available letters: bcdefghijklmnopqrstuvwxyz
Please guess a letter: c
Oops! That letter is not in my word: a_ _ _ _
-----------------------------------
You have 5 guesses left.
Available letters: bdefghijklmnopqrstuvwxyz
Please guess a letter: d
Oops! That letter is not in my word: a_ _ _ _
-----------------------------------
You have 4 guesses left.
Available letters: befghijklmnopqrstuvwxyz
Please guess a letter: e
Good guess: a_ _ _ e
-----------------------------------
You have 4 guesses left.
Available letters: bfghijklmnopqrstuvwxyz
Please guess a letter: f
Oops! That letter is not in my word: a_ _ _ e
-----------------------------------
You have 3 guesses left.
Available letters: bghijklmnopqrstuvwxyz
Please guess a letter: g
Oops! That letter is not in my word: a_ _ _ e
-----------------------------------
You have 2 guesses left.
Available letters: bhijklmnopqrstuvwxyz
Please guess a letter: h
Oops! That letter is not in my word: a_ _ _ e
-----------------------------------
You have 1 guesses left.
Available letters: bijklmnopqrstuvwxyz
Please guess a letter: i
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 19/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
Congratulations all of your hardwork paid of, you have just created the Hangman Game
now go ahead and play the game you created, run the cell below
Wohooo! You did a really good job. We hope you enjoyed your first mini python project here.
We understand it was bit challenging and time taking, but it's worth it, coz here you are to achieve your goal and this course will help you in all
posible way to make your dreams come true.
FeedBack
We hope you’ve enjoyed this course so far. We’re committed to help you use "AI for All" course to its full potential, so that you have a great
learning experience. And that’s why we need your help in form of a feedback here.
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 21/22
14/09/2022, 18:01 Python Intermediate_withoutcode.ipynb - Colaboratory
https://colab.research.google.com/drive/1eU-znTXCPFkpZbN60vlfr97PtRhDNBGZ?authuser=1#scrollTo=OXfcylbvWtgc&printMode=true 22/22