Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

pythonprog

The document describes multiple programming tasks involving operations on binary strings, password validation, arithmetic operations, and array manipulations. Each task includes a function definition, input/output specifications, and example cases. The document also provides sample code implementations for each function in Python.

Uploaded by

uditshahu.21
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

pythonprog

The document describes multiple programming tasks involving operations on binary strings, password validation, arithmetic operations, and array manipulations. Each task includes a function definition, input/output specifications, and example cases. The document also provides sample code implementations for each function in Python.

Uploaded by

uditshahu.21
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Problem Description :

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:

– A denotes AND operation


– B denotes OR operation
– C denotes XOR Operation
You are required to calculate the result of the string str, scanning the string to
right taking one opearation at a time, and return the same.

Note:

No order of priorities of operations is required


Length of str is odd
If str is NULL or None (in case of Python), return -1
Input:
str: 1C0C1C1A0B1

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))

You are given a function.


int CheckPassword(char str[], int n);
The function accepts string str of size n as an argument. Implement the function
which returns 1 if given string str is valid password else 0.
str is a valid password if it satisfies the below conditions.

– 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))

Implement the following Function

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:

n>0 and m>0


Sum lies between integral range
Example

Input
n:4
m:20
Output
90

Explanation

Sum of numbers divisible by 4 are 4 + 8 + 12 + 16 + 20 = 60


Sum of numbers not divisible by 4 are 1 +2 + 3 + 5 + 6 + 7 + 9 + 10 + 11 + 13 + 14
+ 15 + 17 + 18 + 19 = 150
Difference 150 – 60 = 90
Sample Input
n:3
m:10
Sample Output
19

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))

You are required to implement the following Function

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:

All array elements are unique


Treat the 0th position as even
NOTE

Return 0 if array is empty


Return 0, if array length is 3 or less than 3
Example

Input

arr:3 2 1 7 5 4

Output

Explanation

Second largest among even position elements(1 3 5) is 3


Second smallest among odd position element is 4
Thus output is 3+4 = 7
Sample Input

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])

Implement the following functions.a

char*MoveHyphen(char str[],int n);

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.

NOTE:- Return null if str is null.

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)

You are required to implement the following function.

Int OperationChoices(int c, int n, int a , int b )

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.

# Palindrome Number Checking


first_number = int(input())
second_number = int(input())
for i in range(first_number, second_number+1):
reverse = 0
temp = i
while temp != 0:
remainder = temp % 10
reverse = (reverse * 10)+remainder
temp = temp // 10
if i == reverse:
print(reverse, end=" ")

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:

Array index starts with 0


Maximum element and its index should be separated by a line in the output
Assume there is only 1 maximum element in the array
Print exactly what is asked, do not print any additional greeting messages
Example:

Input:
23 45 82 27 66 12 78 13 71 86

Output:

86

Explanation:

86 is the maximum element of the array at index 9.

You might also like