Python Programs

Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Vous êtes sur la page 1sur 3

1.

PlusMius

n = int(input().strip())
list = list(map(int,input().rstrip().split()))
pos = 0
neg = 0
zero = 0
for i in list:
if i > 0:
pos += 1
elif i < 0:
neg += 1
else:
zero += 1
#print(pos,neg,zero)
n_pos = pos/n
n_neg = neg/n
n_zero = zero/n
print(format(n_pos, '5f'))
print(format(n_neg, '5f'))
print(format(n_zero, '5f'))

---------------------------------------------------------------------------------------------------
2. MaxMin

def miniMaxSum(arr):
min_val = min(arr)
max_val = max(arr)
total_sum = sum(arr)

min_sum = total_sum - max_val


max_sum = total_sum - min_val

print(min_sum, max_sum)

arr = list(map(int, intput("Enter values of array: ").rstrip().split()))


miniMaxSum(arr)

---------------------------------------------------------------------------------------------------

3. TimeConversion

def timeConversion(s):
# Extract hour, minutes, seconds, and period (AM/PM)
hour, minutes, seconds = map(int, s[:-2].split(':'))
period = s[-2:]

# Adjust hour for PM


if period == 'PM' and hour != 12:
hour += 12

# Adjust hour for AM and 12 AM


if period == 'AM' and hour == 12:
hour = 0

# Format output time in 24-hour format


military_time = '{:02d}:{:02d}:{:02d}'.format(hour, minutes, seconds)
return military_time

# Example usage
s = input("Enter time in 12-hour AM/PM format: ")
result = timeConversion(s)
print("Time in 24-hour format:", result)

---------------------------------------------------------------------------------------------------

4. Lonely Integer

def lonelyinteger(a):
# Write your code here
for i in a:
x = a.count(i)
if x == 1:
f=i
elif x > 1:
continue
return f

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = int(input().strip())

a = list(map(int, input().rstrip().split()))

result = lonelyinteger(a)

fptr.write(str(result) + '\n')

fptr.close()

---------------------------------------------------------------------------------------------------

5. Absolute Diagonal Difference

def diagonalDifference(arr):
x = 0
y = 0
n = len(arr)

for i in range(n):
x += arr[i][i]
y += arr[i][n - i - 1]

absolute_difference = abs(x - y)

return absolute_difference

n = int(input().strip())
arr = []

for _ in range(n):
arr.append(list(map(int, input().rstrip().split())))

print(diagonalDifference(arr))

---------------------------------------------------------------------------------------------------

6.

Vous aimerez peut-être aussi