pythonprog
pythonprog
The Binary number system only uses two digits, 0 and 1 and number system can be
called binary string. You are required to implement the following function:
int OperationsBinaryString(char* str);
The function accepts a string str as its argument. The string str consists of
binary digits eparated with an alphabet as follows:
Note:
Output:
1
def OperationsBinaryString(str):
a=int(str[0])
i=1
while i< len(str):
if str[i]=='A':
a&=int(str[i+1])
elif str[i]=='B':
a|=int(str[i+1])
else:
a^=int(str[i+1])
i+=2
return a
str=input()
print(OperationsBinaryString(str))
– At least 4 characters
– At least one numeric digit
– At Least one Capital Letter
– Must not have space or slash (/)
– Starting character must not be a number
Assumption:
Input string will not be empty.
Example:
Input 1:
aA1_67
Input 2:
a987 abC012
Output 1:
1
Output 2:
0
def CheckPassword(s,n):
if n<4:
return 0
if s[0].isdigit():
return 0
cap=0
nu=0
for i in range(n):
if s[i]==' ' or s[i]=='/':
return 0
if s[i]>='A' and s[i]<='Z':
cap+=1
elif s[i].isdigit():
nu+=1
if cap>0 and nu>0:
return 1
else:
return 0
s=input()
a=len(s)
print(CheckPassword(s,a))
def differenceofSum(n. m)
The function 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.
Assumption:
Input
n:4
m:20
Output
90
Explanation
n = int(input())
m = int(input())
sum1 = 0
sum2 = 0
for i in range(1,m+1):
if i % n == 0:
sum1+=i
else:
sum2+=i
print(abs(sum2-sum1))
def LargeSmallSum(arr)
The function accepts an integers arr of size ’length’ as its arguments you are
required to return the sum of second largest element from the even positions and
second smallest from the odd position of given ‘arr’
Assumption:
Input
arr:3 2 1 7 5 4
Output
Explanation
arr:1 8 0 2 3 5 6
Sample Output
length = int(input())
arr = list(map(int, input().split()))
even_arr = []
odd_arr = []
for i in range(length):
if i % 2 == 0:
even_arr.append(arr[i])
else:
odd_arr.append(arr[i])
even_arr = sorted(even_arr)
odd_arr = sorted(odd_arr)
print(even_arr[len(even_arr)-2] + odd_arr[len(odd_arr)-2])
The function accepts a string “str” of length ‘n’, that contains alphabets and
hyphens (-). Implement the function to move all hyphens(-) in the string to the
front of the given string.
Example :-
Input:
str.Move-Hyphens-to-Front
Output:
—MoveHyphenstoFront
Explanation:-
The string “Move-Hyphens -to-front” has 3 hyphens (-), which are moved to the front
of the string, this output is “— MoveHyphen”
Sample Input
Str: String-Compare
Sample Output-
-StringCompare
inp = input()
count = 0
final = ""
for i in inp:
if i == '-':
count+=1
else:
final+=i
print("-"*count,final)
The function accepts 3 positive integers ‘a’ , ‘b’ and ‘c ‘ as its arguments.
Implement the function to return.
( a+ b ) , if c=1
( a – b ) , if c=2
( a * b ) , if c=3
(a / b) , if c =4
Assumption : All operations will result in integer output.
Example:
Input
c :1
a:12
b:16
Output:
Since ‘c’=1 , (12+16) is performed which is equal to 28 , hence 28 is returned.
Sample Input
c : 2
a : 16
b : 20
Sample Output
-4
def operationChoices(c,a,b):
if c == 1 :
return(a+b)
elif c == 2:
return(a-b)
elif c == 3:
return(a*b)
else:
return(a//b)
c,a,b = map(int,input().split())
print(operationChoices(c, a, b))
Instructions: You are required to write the code. You can click on compile and run
anytime to check compilation/execution status. The code should be
logically/syntactically correct.
Problem: Write a program in python to display the table of a number and print the
sum of all the multiples in it.
Test Cases:
Test Case 1:
Input:
5
Expected Result Value:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50
275
Test Case 2:
Input:
12
Expected Result Value:
12, 24, 36, 48, 60, 72, 84, 96, 108, 120
660
table_number = int(input())
sum = 0
for i in range(1, 11):
value = table_number * i
print(value, end=" ")
sum = sum + value
print()
print(sum)
Instructions: You are required to write the code. You can click on compile and run
anytime to check compilation/execution status. The code should be
logically/syntactically correct.
Question: Write a program in C such that it takes a lower limit and upper limit as
inputs and print all the intermediate palindrome numbers.
Test Cases:
TestCase 1:
Input :
10 , 80
Expected Result:
11 , 22 , 33 , 44 , 55 , 66 , 77.
Test Case 2:
Input:
100,200
Expected Result:
101 , 111 , 121 , 131 , 141 , 151 , 161 , 171 , 181 , 191.
Problem Statement :
You are given a function, void MaxInArray(int arr[], int length); The function
accepts an integer array ‘arr’ of size ‘length’ as its argument. Implement the
function to find the maximum element of the array and print the maximum element and
its index to the standard output
(STDOUT). The maximum element and its index should be printed in separate lines.
Note:
Input:
23 45 82 27 66 12 78 13 71 86
Output:
86
Explanation: