Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comments to Apple_and_Orange.py #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 26 additions & 17 deletions HackerRank-Apple and Orange/Apple_and_Orange.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,51 @@

# Complete the countApplesAndOranges function below.
def countApplesAndOranges(s, t, a, b, apples, oranges):
# Initialize counters for apples and oranges that fall within the house's range [s, t].
acount = 0
bcount = 0

# Iterate through the apples' positions.
for i in range(len(apples)):
temp = a+apples[i]
if(temp in range(s,t+1)):
acount+=1
# Calculate the actual position of the apple relative to the tree a.
temp = a + apples[i]
# Check if the apple's position falls within the house's range [s, t].
if temp in range(s, t + 1):
# If the apple falls within the house's range, increment the apple counter.
acount += 1

# Iterate through the oranges' positions.
for i in range(len(oranges)):
temp = b+oranges[i]
if(temp in range(s,t+1)):
bcount+=1
print (acount)
print (bcount)


# Calculate the actual position of the orange relative to the tree b.
temp = b + oranges[i]
# Check if the orange's position falls within the house's range [s, t].
if temp in range(s, t + 1):
# If the orange falls within the house's range, increment the orange counter.
bcount += 1

# Print the final count of apples and oranges that fall within the house's range.
print(acount)
print(bcount)

if __name__ == '__main__':
# Read the input for the house's range [s, t].
st = input().split()

s = int(st[0])

t = int(st[1])

# Read the input for the positions of trees a and b.
ab = input().split()

a = int(ab[0])

b = int(ab[1])

# Read the input for the number of apples and oranges.
mn = input().split()

m = int(mn[0])

n = int(mn[1])

# Read the input for the positions of apples and oranges as lists.
apples = list(map(int, input().rstrip().split()))

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

# Call the countApplesAndOranges function with the given inputs.
countApplesAndOranges(s, t, a, b, apples, oranges)