Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions CompetitiveProgramming/CodeChef/P40_COINS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# In Byteland they have a very strange monetary system.
#
# Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into
# three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).
#
# You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy
# Bytelandian coins.
#
# You have one gold coin. What is the maximum amount of American dollars you can get for it?
#
# Input
# The input will contain several test cases (not more than 10). Each testcase is a single line with a number
# n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
#
# Output
# For each test case output a single line, containing the maximum amount of American dollars you can make.
#
# Example
# Input:
# 12
# 2
#
# Output:
# 13
# 2
# You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try changing the
# coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them.
# It is better just to change the 2 coin directly into $2.

import sys

list={}
def chk(n):
if n in list.keys():
return list[n]
if n<=2:
ans = n
else:
ans = chk(n//2) + chk(n//3) + chk(n//4)
if ans<n : ans = n
list[n] = ans
return ans

for case in sys.stdin:
n = int(case)
print(chk(n))
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# You are given container full of water. Container can have limited amount of water. You also have
# N bottles to fill. You need to find the maximum numbers of bottles you can fill.
#
# Input:
# First line contains one integer, T, number of test cases.
# First line of each test case contains two integer, N and X, number of bottles and capacity of the container.
# Second line of each test case contains
# N space separated integers, capacities of bottles.
#
# Output:
# For each test case print the maximum number of bottles you can fill.
#
# Constraints:
# 1≤T≤100
# 1≤N≤104
# 1≤X≤109
#
# SAMPLE INPUT
# 1
# 5 10
# 8 5 4 3 2
#
# SAMPLE OUTPUT
# 3

for _ in range(int(input())):
N, capacity = map(int, input().split())
capacities = [int(bottle) for bottle in input().split()]
capacities.sort()
check, bottles = 0, 0
for i in range(len(capacities)):
if check > capacity:
bottles -= 1
break
bottles += 1
check += capacities[i]

print(bottles)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Given a string S, and two numbers N, M - arrange the characters of string in between the indexes N
# and M (both inclusive) in descending order. (Indexing starts from 0).
#
# Input Format:
# First line contains T - number of test cases.
# Next T lines contains a string(S) and two numbers(N, M) separated by spaces.
#
# Output Format:
# Print the modified string for each test case in new line.
#
# Constraints:
#
# 1≤T≤1000
# 1≤|S|≤10000 // |S| denotes the length of string.
# 0≤N≤M<|S|
# S∈[a,z]
#
# SAMPLE INPUT
# 3
# hlleo 1 3
# ooneefspd 0 8
# effort 1 4
#
# SAMPLE OUTPUT
# hlleo
# spoonfeed
# erofft

for i in range(int(input())):
user_input = input()
user_input = user_input.split()
to_char = int(user_input[2])
from_char = int(user_input[1])
string = user_input[0][from_char:to_char + 1]
replace = ''.join(sorted(string)[::-1])
print(user_input[0][:from_char] + replace + user_input[0][to_char + 1:len(user_input[0])])
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Given a string containing only lower case letters ,print first occurrence of all the letters present
# in that order only.
#
# Input :
#
# Test cases, t
# string ,s
# Output :
#
# Desired Output
#
# Constraint :
#
# string length <=200
#
# SAMPLE INPUT
# 2
# aasdvasvavda
# sajhags
#
# SAMPLE OUTPUT
# asdv
# sajhg
#

for _ in range(int(input())):
user_input = input()
string = []
for i in range(len(user_input)):
if user_input[i] not in string:
string.append(user_input[i])

print(''.join(string))
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Monk introduces the concept of palindrome saying,"A palindrome is a sequence of characters which reads the s
# ame backward or forward."
# Now, since he loves things to be binary, he asks you to find whether the given string is palindrome or not.
# If a given string is palindrome, you need to state that it is even palindrome (palindrome with even length)
# or odd palindrome (palindrome with odd length).
#
# Input:
# The first line consists of T, denoting the number of test cases.
# Next follow T lines, each line consisting of a string of lowercase English alphabets.
#
# Output:
# For each string , you need to find whether it is palindrome or not.
# If it is not a palindrome, print NO.
# If it is a palindrome, print YES followed by a space; then print EVEN it is an even palindrome
# else print ODD.
# Output for each string should be in a separate line.
# See the sample output for clarification.
#
# Constraints:
# 1≤T≤50
# 1≤length of string≤105
#
# SAMPLE INPUT
# 3
# abc
# abba
# aba
#
# SAMPLE OUTPUT
# NO
# YES EVEN
# YES ODD

for _ in range(int(input())):
user_input = input()
if user_input == user_input[::-1]:
if len(user_input) % 2 == 0:
print('YES EVEN')
else:
print('YES ODD')
else:
print('NO')
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Everyone is familiar with Pratik's obsession with DNA and how much he likes to find the correct pair for
# the nucleotide bases. One day Samim found him exaggerating about his knowledge of DNA bases. So Samim
# challenged Pratik about finding the correct base pair for the given DNA sequence and show the result.
# Also he secretly introduced some of RNA nucleotide bases to test Pratik. Now initially he accepted the
# challenge but soon found out about how big the sequence actually was, so he came to you ask him for your
# in finding the sequence and keep his pride about the knowledge of DNA molecules.
#
# You are given a string that contains the nucleotide bases of DNA and RNA, you are needed to find the
# correct pair for all the bases and print the corresponding sequence obtained. In case the sequence
# contains a RNA base, print "Error RNA nucleobases found!" (without quotes).
#
# INPUT FORMAT
#
# The first line of input contains T, the no of test cases. The next line of input contains N, the no of
# bases in each of the DNA sequence The line after that contains the DNA sequence.
#
# OUTPUT FORMAT
#
# For each test case output your answer on a new line.
#
# CONSTRAIN
#
# 1≤T≤10^4
# 1≤N≤10^6
#
# SAMPLE INPUT
# 3
# 2
# AG
# 4
# ATGC
# 6
# UGCACT
#
# SAMPLE OUTPUT
# TC
# TACG
# Error RNA nucleobases found!

for _ in range(int(input())):
string_length = int(input())
string = input()
check = {'A':'T', 'T':'A', 'G':'C', 'C':'G'}
result = []

if 'U' in string:
print('Error RNA nucleobases found!')
else:
for i in range(len(string)):
result.append(check[string[i]])

print(''.join(result))
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Manish has got the task to frame a speech for his professor at the university at the Annual sports meet.But
# the problem is that the professor has speech dyslexia and he can't speak the words clearly which have vowels
# in them. So Manish has to avoid such words and has to minimise their usage in the speech letter. Your task
# is to help Manish mark the vowels in the words so that he can minimise their use. You are given a string S
# consisting of lower case letters only. You need to count the number of vowels in the string S.
#
# INPUT The first line will contain an integer T denoting the number of test cases. The following T lines
# will contain a string S in lower case letters only.
#
# OUTPUT Print the number the vowels in the string S.
#
# CONSTRAINTS 1<=T<=100
#
# SAMPLE INPUT
# 1
# hashes
#
# SAMPLE OUTPUT
# 2

for _ in range(int(input())):
string = input()
count = 0
for i in range(len(string)):
if string[i] in ['a', 'e', 'i', 'o', 'u']:
count += 1

print(count)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Doraemon gave Nobita a gadget that swaps words inside a string in the following manner :
#
# If there are W words, word 1 is swapped with word W, word 2 is swapped with word W-1 and so on. The
# problem is that Nobita himself cannot verify the answer for large strings. Help him write a program to do so.
#
# INPUT :
# the first line of the input contains the number of test cases. Each test case consists of a single line
# containing the string.
#
# OUTPUT :
# output the string with the words swapped as stated above.
#
# CONSTRAINTS :
# |string length| <= 100000
# string contains english alphabets and spaces
#
# SAMPLE INPUT
# 1
# hello world
#
# SAMPLE OUTPUT
# world hello

for _ in range(int(input())):
string = input().split()
print(' '.join(string[::-1]))
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Given a string 'S' , u need to tell whether it is 'sumit's string or not'.
#
# A string is called 'Sumit's String' , if distance between adjacent character is 1.
#
# Consider that the alphabets are arranged in cyclic manner from 'a' to 'z'. distance between any character
# 'x' and 'y' will be defined as minimum number of steps it takes 'x' to reach 'y'. Here, character 'x' can
# start moving clockwise or anti-clockwise in order to reach at position where character 'y' is placed.
#
# Print 'YES' if it is Sumit's string else print 'NO', for each yest case.
#
# Input :
#
# test cases, t
# string , s
# Output:
#
# Desired O/p
#
# Constraints :
#
# string length<=250
# string has only lower case letters
#
# SAMPLE INPUT
# 3
# aba
# zza
# bcd
#
# SAMPLE OUTPUT
# YES
# NO
# YES

for _ in range(int(input())):
string = input()
sumit_string = True
check = 'abcdefghijklmnopqrstuvwxyz'
for i in range(len(string) - 1):
check_first = check.find(string[i]) + 1
check_second = check.find(string[i + 1]) + 1
if abs(check_second - check_first) == 1 or abs(check_second - check_first) == 25:
continue
else:
sumit_string = False
break

if sumit_string:
print('YES')
else:
print('NO')
Loading