0% found this document useful (0 votes)
3 views

accenture-new-coding

Uploaded by

hemu230879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

accenture-new-coding

Uploaded by

hemu230879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Food Festival at HillTown

HillTown Inn is planning to organize a Food Festival bringing together at one


place, a wide variety of cuisines from across the world on account of Christmas.The
Hotel Management has rented out a square hall of an indoor Auditorium for this
extravaganza. The side of the square hall is y inches in which a large square table
is placed for the display of the most popular and celebrated food items. The side
of the square table is x inches, such that x<y.The Management wanted to fill the
remaining floor area with a decorative carpet. To get this done, they needed to
know the floor area to be filled with the carpet. Write a program to help the
Management find the area of the region located outside the square table, but inside
the square hall.

Input Format
First line of the input is an integer y, the side of the square hall.
Second line of the input is an integer x, the side of the square table placed for
display.
Output Format
Output should display the area of the floor that is to be decorated with the
carpet.
Refer sample input and output for formatting specifications.

Sample Input 1
7
3
Sample Output 1
40
Sample Input 2
5
2
Sample Output 2
21

Solution
sh = int(input())
st = int(input())
print(sh**2-st**2)

Testcase: 1
Input
5
2
Output
21
*********************************************************************
WonderWorks Magic Show

The Magic Castle, the home of the Academy of Magical Arts at California has
organized the great ‘WonderWorks Magic Show’. 3 renowned magicians were invited to
mystify and thrill the crowd with their world’s spectacular magic tricks. At the
end of each of the 3 magicians’ shows, the audience were requested to give their
feedback in a scale of 1 to 10. Number of people who watched each show and the
average feedback rating of each show is known. Write a program to find the average
feedback rating of the WonderWorks Magic show.

Input Format
First line of the input is an integer value that corresponds to the number of
people who watched show 1.
Second line of the input is a float value that corresponds to the average rating of
show 1.
Third line of the input is an integer value that corresponds to the number of
people who watched show 2.
Fourth line of the input is a float value that corresponds to the average rating of
show 2.
Fifth line of the input is an integer value that corresponds to the number of
people who watched show 3.
Sixth line of the input is a float value that corresponds to the average rating of
show 3.
Constraints
0<=Average rating <=10
Sample Input 1
400
9.8
500
9.6
100
5
Sample Output 1
9.22

Solution

show = [0]*3
rat = [0]*3
for i in range(0,3):
show[i] = int(input())
rat[i] = float(input())
rat[i] = show[i]*rat[i]
print(round((sum(rat)/sum(show)),2))
Testcase:
Input
4000
9
500
9
100
9

Output
9.0
******************************************************************

********************************************************************
3)def are_anagrams(str1, str2):
# Remove spaces and convert both strings to lowercase
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()

# Check if the sorted versions of the strings are equal


if sorted(str1) == sorted(str2):
return 'yes'
else:
return 'no'

# Example usage:
string1 = "listen"
string2 = "silent"
result = are_anagrams(string1, string2)
print(result)
*********************************************************************
4)def swap_characters(str, ch1, ch2):
# Replace ch1 with a temporary unique character
temp_char = '###'
str = str.replace(ch1, temp_char)
# Replace ch2 with ch1
str = str.replace(ch2, ch1)
# Replace the temporary character with ch2
str = str.replace(temp_char, ch2)

return str

input_str = "Hello, world!"


ch1 = 'o'
ch2 = 'l'
result = swap_characters(input_str, ch1, ch2)
print(result)

output:
Heool wlrod
*******************************************************************
5)def reverse_string_word_wise(input_str):
# Split the input string into words
words = input_str.split()
# Reverse the order of words
reversed_words = words[::-1]
# Join the reversed words to form the final string
reversed_str = ' '.join(reversed_words)
return reversed_str

# Example usage:
input_str = "Hello, world!"
result = reverse_string_word_wise(input_str)
print(result)
# Output: "world! Hello,"
*******************************************************************
6)def is_palindrome(input_str):
# Remove spaces and convert the string to lowercase
input_str = input_str.replace(" ", "").lower()

# Check if the string is equal to its reverse


return input_str == input_str[::-1]

# Example usage:
string1 = "racecar"
string2 = "hello"
result1 = is_palindrome(string1)
result2 = is_palindrome(string2)
print(result1) # Output: 1 (True)
print(result2)
*******************************************************************
import re

def check_password(password, n=8):


# Check if the password meets the minimum length requirement
if len(password) < n:
return False

# Check if the password contains at least one uppercase letter, one lowercase
letter, and one digit
if not re.search(r'[A-Z]', password) or not re.search(r'[a-z]', password) or
not re.search(r'\d', password):
return False

return True

# Example usage:
password = "Password123" # Example password
if check_password(password, 8):
print("Password is valid!")
else:
print("Password is invalid.")
*****************************************************************
Arrays:
def find_intersection(arr1, arr2):
set1 = set(arr1)
set2 = set(arr2)
intersection = set1.intersection(set2)
return list(intersection)

# Example usage:
arr1 = [1, 2, 3, 4, 5]
arr2 = [3, 4, 5, 6, 7]
result = find_intersection(arr1, arr2)
print(result)
# Output: [3, 4, 5]
*********************************************************************
def merge_sorted_arrays(arr1, arr2):
merged_array = []
i, j = 0, 0

while i < len(arr1) and j < len(arr2):


if arr1[i] < arr2[j]:
merged_array.append(arr1[i])
i += 1
else:
merged_array.append(arr2[j])
j += 1

# If there are remaining elements in arr1 or arr2, append them


while i < len(arr1):
merged_array.append(arr1[i])
i += 1

while j < len(arr2):


merged_array.append(arr2[j])
j += 1

return merged_array

# Example usage:
arr1 = [1, 3, 5, 7]
arr2 = [2, 4, 6, 8]
result = merge_sorted_arrays(arr1, arr2)
print(result)
# Output: [1, 2, 3, 4, 5, 6, 7, 8]
**********************************************************************
def remove_duplicates(nums):
if not nums:
return 0 # Return 0 for an empty array

unique_count = 1 # At least one element (the first one) is unique

for i in range(1, len(nums)):


if nums[i] != nums[i - 1]:
nums[unique_count] = nums[i]
unique_count += 1

return unique_count

# Example usage:
nums = [1, 1, 2, 2, 2, 3, 4, 4, 5]
new_length = remove_duplicates(nums)
print(nums[:new_length]) # Output: [1, 2, 3, 4, 5]
*******************************************************************
def reverse_array(nums, start, end):
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1

def rotate_array(nums, k):


n = len(nums)
k = k % n # To handle cases where k > n

# Reverse the entire array


reverse_array(nums, 0, n - 1)

# Reverse the first k elements


reverse_array(nums, 0, k - 1)

# Reverse the remaining elements


reverse_array(nums, k, n - 1)

# Example usage:
nums = [1, 2, 3, 4, 5, 6, 7]
k = 3 # Rotate by 3 steps
rotate_array(nums, k)
print(nums)
# Output: [5, 6, 7, 1, 2, 3, 4]
************************************************************************
def max_subarray_sum(arr):
max_ending_here = max_so_far = arr[0]

for num in arr[1:]:


max_ending_here = max(num, max_ending_here + num)
max_so_far = max(max_so_far, max_ending_here)

return max_so_far

# Example usage:
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
result = max_subarray_sum(arr)
print(result)
# Output: 6 (The largest sum is from [4, -1, 2, 1])
************************************************************************

You might also like