The Joy of Computing using Python Week 8 - Programming Assignment 1,2 and 3 | NPTEL | Answer with Explanation
The Joy of Computing using Python - Course | NPTEL - All assignment
The Joy of Computing using Python Week 8: Programming Assignment 1
Given a Tuple T, create a function squareT that accepts one argument and returns a tuple containing similar elements given in tuple T and its sqaures in sequence.
Input: (1,2,3) Output : (1,2,3,1,4,9) Explanation: Tuple T contains (1,2,3) the output tuple contains the original elements of T that is 1,2,3 and its sqaures 1,4,9 in sequence.
Answer
def squareT(T): return T + tuple(x**2 for x in T)Given:
if __name__ == "__main__": n = int(input()) L = list(map(int, input().split())) T = tuple(L) ans = squareT(T) if type(ans) == type(T): print(ans)
The Joy of Computing using Python Week 8: Programming Assignment 2
Given a string S, write a function replaceV that accepts a string and replace the occurrences of 3 consecutive vowels with _ in that string. Make sure to return and print the answer string.
Input: aaahellooo Output: _hell_ Explanation: Since aaa and ooo are consecutive 3 vowels and therefor replaced by _ .
Answer
def isvowel(ch): v = 'AEIOUaeiou' if ch in v: return True else: return False def replaceV(s): n = len(s) ans = '' i=0 while(i<n-2): if isvowel(s[i]) and isvowel(s[i+1]) and isvowel(s[i+2]): ans = ans+'_' i = i+3 else: ans = ans+s[i] i = i+1 return ans+s[i:] S = input() print(replaceV(S))
You May Like to Learn - Python - Scripting Language
The Joy of Computing using Python Week 8: Programming Assignment 3
Write a program that take list L as an input and shifts all zeroes in list L towards the right by maintaining the order of the list. Also print the new list.
Input: [0,1,0,3,12] Output: [1,3,12,0,0] Explanation: There are two zeroes in the list which are shifted to the right and the order of the list is also maintained. (1,3,12 are in order as in the old list.)
Answer
L = list(map(int, input().split())) zeroes = L.count(0) i = 0 while i < len(L)-zeroes: if L[i] == 0: L.pop(i) L.append(0) i -= 1 i += 1 print(L,end="")
Disclaimer:
"This page contains multiple choice questions (MCQs) related to The Joy of Computing using Python . The answers to these questions are provided for educational and informational purposes only.These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself."
0 Comments