Soft Skills

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1.Two friends Anna and Brian, are deciding how to split the bill at a dinner.

Each will only pay for the items


they consume. Brian gets the check and calculates Anna's portion. You must determine if his calculation is
correct.

For example, assume the bill has the following prices: . Anna declines to eat item which costs . If Brian
calculates the bill correctly, Anna will pay . If he includes the cost of , he will calculate . In the second case, he
should refund to Anna.

Function Description

Complete the bonAppetit function in the editor below. It should print Bon Appetit if the bill is fairly split.
Otherwise, it should print the integer amount of money that Brian owes Anna.

bonAppetit has the following parameter(s):

• bill: an array of integers representing the cost of each item ordered

• k: an integer representing the zero-based index of the item Anna doesn't eat

• b: the amount of money that Anna contributed to the bill

Input Format

The first line contains two space-separated integers and , the number of items ordered and the -based index
of the item that Anna did not eat.
The second line contains space-separated integers where .
The third line contains an integer, , the amount of money that Brian charged Anna for her share of the bill.

Constraints

• The amount of money due Anna will always be an integer

Output Format

If Brian did not overcharge Anna, print Bon Appetit on a new line; otherwise, print the difference (i.e., ) that
Brian must refund to Anna. This will always be an integer.

Sample Input 0

41

3 10 2 9

12

Sample Output 0

Explanation 0
Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items
is and, split in half, the cost per person is . Brian charged her for her portion of the bill. We print the amount
Anna was overcharged, , on a new line.
Sample Input 1

41

3 10 2 9

Sample Output 1

Bon Appetit

Explanation 1
Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items
is and, split in half, the cost per person is . Because , we print Bon Appetit on a new line.

Contest ends in 3 minutes

Submissions:

44

Max Score:

10

Difficulty:

Easy

Rate This Challenge:

More

Python 3

import math

import os

import random

import re

import sys

# Complete the 'bonAppetit' function below.

#
# The function accepts following parameters:

# 1. INTEGER_ARRAY bill

# 2. INTEGER k

# 3. INTEGER b

def bonAppetit(bill, k, b):

total = sum(bill) - bill[k]

# Calculate Anna's share

anna_share = total // 2

# If the amount Anna paid is correct, print "Bon Appetit"

if anna_share == b:

print("Bon Appetit")

else:

# Otherwise, print the difference Brian owes Anna

print(b - anna_share)

if __name__ == '__main__':

first_multiple_input = input().rstrip().split()

n = int(first_multiple_input[0])

k = int(first_multiple_input[1])

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


b = int(input().strip())

bonAppetit(bill, k, b)

2.

Louise joined a social networking site to stay in touch with her friends. The signup page required her to input
a name and a password. However, the password must be strong. The website considers a password to
be strong if it satisfies the following criteria:

• Its length is at least .

• It contains at least one digit.

• It contains at least one lowercase English character.

• It contains at least one uppercase English character.

• It contains at least one special character. The special characters are: !@#$%^&*()-+

She typed a random string of length in the password field but wasn't sure if it was strong. Given the string she
typed, can you find the minimum number of characters she must add to make her password strong?

Note: Here's the set of types of characters in a form you can paste in your solution:

numbers = "0123456789"

lower_case = "abcdefghijklmnopqrstuvwxyz"

upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

special_characters = "!@#$%^&*()-+"

Example

This password is 5 characters long and is missing an uppercase and a special character. The minimum number
of characters to add is .

This password is 5 characters long and has at least one of each character type. The minimum number of
characters to add is .

Function Description

Complete the minimumNumber function in the editor below.

minimumNumber has the following parameters:

• int n: the length of the password

• string password: the password to test


Returns

• int: the minimum number of characters to add

Input Format

The first line contains an integer , the length of the password.

The second line contains the password string. Each character is either a lowercase/uppercase English
alphabet, a digit, or a special character.

Constraints

• All characters in are in [a-z], [A-Z], [0-9], or [!@#$%^&*()-+ ].

Sample Input 0

Ab1

Sample Output 0

Explanation 0

She can make the password strong by adding characters, for example, $hk, turning the password
into Ab1$hk which is strong.

characters aren't enough since the length must be at least .

Sample Input 1

11

#HackerRank

Sample Output 1

Explanation 1

The password isn't strong, but she can make it strong by adding a single digit.

Contest ends in a minute

Submissions:

41

Max Score:

10

Difficulty:

Easy
Rate This Challenge:

More

Python 3

#!/bin/python3

import math

import os

import random

import re

import sys

# Complete the 'minimumNumber' function below.

# The function is expected to return an INTEGER.

# The function accepts following parameters:

# 1. INTEGER n

# 2. STRING password

def minimumNumber(n, password):

# Define character sets

numbers = "0123456789"

lower_case = "abcdefghijklmnopqrstuvwxyz"

upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

special_characters = "!@#$%^&*()-+"
# Initialize flags for required character types

has_digit = has_lower = has_upper = has_special = False

# Check each character in the password to see which types are present

for char in password:

if char in numbers:

has_digit = True

elif char in lower_case:

has_lower = True

elif char in upper_case:

has_upper = True

elif char in special_characters:

has_special = True

# Count missing types

missing_types = 0

if not has_digit:

missing_types += 1

if not has_lower:

missing_types += 1

if not has_upper:

missing_types += 1

if not has_special:

missing_types += 1

# The password must be at least 6 characters long

# So we need either the missing types or enough characters to make the length 6

return max(missing_types, 6 - n)

# Driver code to take input and print output

if __name__ == '__main__':

n = int(input()) # Read the length of the password


password = input() # Read the password string

# Call the function and print the result

print(minimumNumber(n, password))

3.

Dothraki are planning an attack to usurp King Robert's throne. King Robert learns of this conspiracy from
Raven and plans to lock the single door through which the enemy can enter his kingdom.

But, to lock the door he needs a key that is an anagram of a palindrome. He starts to go through his box of
strings, checking to see if they can be rearranged into a palindrome. Given a string, determine if it can be
rearranged into a palindrome. Return the string YES or NO.

Example

One way this can be arranged into a palindrome is . Return YES.

Function Description
Complete the gameOfThrones function below.

gameOfThrones has the following parameter(s):

• string s: a string to analyze

Returns

• string: either YES or NO

Input Format

A single line which contains .

Constraints

• |s|

• contains only lowercase letters in the range

Sample Input 0

aaabbbb
Sample Output 0

YES

Explanation 0

A palindromic permutation of the given string is bbaaabb.

Sample Input 1

cdefghmnopqrstuvw

Sample Output 1

NO

Explanation 1

Palindromes longer than 1 character are made up of pairs of characters. There are none here.

Sample Input 2

cdcdcdcdeeeef

Sample Output 2

YES

Explanation 2

An example palindrome from the string: ddcceefeeccdd.

Contest ends in a few seconds

Submissions:

35

Max Score:

10

Difficulty:

Easy

Rate This Challenge:

More

Python 3

#!/bin/python3
import math

import os

import random

import re

import sys

# Complete the 'gameOfThrones' function below.

# The function is expected to return a STRING.

# The function accepts STRING s as parameter.

from collections import Counter

def gameOfThrones(s):

# Count the frequency of each character in the string

freq = Counter(s)

# Count how many characters have an odd frequency

odd_count = sum(1 for count in freq.values() if count % 2 != 0)

# A string can be rearranged into a palindrome if there's at most one odd frequency

if odd_count > 1:

return "NO"

else:

return "YES"

# Driver code to take input and print output

if __name__ == '__main__':

s = input() # Read the input string

result = gameOfThrones(s) # Call the function


print(result) # Output the result

You might also like