Skip to content

Commit fda1f61

Browse files
Merge pull request #1 from dikshajoshi18/alpha
Alpha
2 parents 452ddc9 + 734a115 commit fda1f61

9 files changed

+368
-27
lines changed
File renamed without changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'''# Problem: https://www.hackerrank.com/challenges/tutorial-intro/problem
2+
# Score: 30
3+
About Tutorial Challenges
4+
Many of the challenges on HackerRank are difficult and assume that you already know the relevant algorithms. These tutorial challenges are different. They break down algorithmic concepts into smaller challenges so that you can learn the algorithm by solving them. They are intended for those who already know some programming, however. You could be a student majoring in computer science, a self-taught programmer, or an experienced developer who wants an active algorithms review. Here's a great place to learn by doing!
5+
6+
The first series of challenges covers sorting. They are listed below:
7+
8+
Tutorial Challenges - Sorting
9+
10+
Insertion Sort challenges
11+
12+
Insertion Sort 1 - Inserting
13+
Insertion Sort 2 - Sorting
14+
Correctness and loop invariant
15+
Running Time of Algorithms
16+
Quicksort challenges
17+
18+
Quicksort 1 - Partition
19+
Quicksort 2 - Sorting
20+
Quicksort In-place (advanced)
21+
Running time of Quicksort
22+
Counting sort challenges
23+
24+
Counting Sort 1 - Counting
25+
Counting Sort 2 - Simple sort
26+
Counting Sort 3 - Preparing
27+
Full Counting Sort (advanced)
28+
There will also be some challenges where you'll get to apply what you've learned using the completed algorithms.
29+
30+
About the Challenges
31+
Each challenge will describe a scenario and you will code a solution. As you progress through the challenges, you will learn some important concepts in algorithms. In each challenge, you will receive input on STDIN and you will need to print the correct output to STDOUT.
32+
33+
There may be time limits that will force you to make your code efficient. If you receive a "Terminated due to time out" message when you submit your solution, you'll need to reconsider your method. If you want to test your code locally, each test case can be downloaded, inputs and expected results, using hackos. You earn hackos as you solve challenges, and you can spend them on these tests.
34+
35+
For many challenges, helper methods (like an array) will be provided for you to process the input into a useful format. You can use these methods to get started with your program, or you can write your own input methods if you want. Your code just needs to print the right output to each test case.
36+
37+
Sample Challenge
38+
This is a simple challenge to get things started. Given a sorted array () and a number (), can you print the index location of in the array?
39+
40+
For example, if and , you would print for a zero-based index array.
41+
42+
If you are going to use the provided code for I/O, this next section is for you.
43+
44+
Function Description
45+
46+
Complete the introTutorial function in the editor below. It must return an integer representing the zero-based index of .
47+
48+
introTutorial has the following parameter(s):
49+
50+
arr: a sorted array of integers
51+
V: an integer to search for
52+
The next section describes the input format. You can often skip it, if you are using included methods or code stubs.
53+
54+
Input Format
55+
56+
The first line contains an integer, , a value to search for.
57+
The next line contains an integer, , the size of . The last line contains space-separated integers, each a value of where .
58+
59+
Output Format
60+
Output the index of in the array.
61+
62+
The next section describes the constraints and ranges of the input. You should check this section to know the range of the input.
63+
64+
Constraints
65+
66+
It is guaranteed that will occur in exactly once.
67+
This "sample" shows the first input test case. It is often useful to go through the sample to understand a challenge.
68+
69+
Sample Input 0
70+
71+
4
72+
6
73+
1 4 5 7 9 12
74+
Sample Output 0
75+
76+
1
77+
Explanation 0
78+
. The value is the element in the array, but its index is since in this case, array indices start from (see array definition under Input Format).
79+
'''
80+
v = int(input())
81+
n = int(input())
82+
arr = list(map(int, input().split()))
83+
i = 0
84+
while arr[i] != v:
85+
i += 1
86+
print(i)

Algorithms/Marc's Cakewalk.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''# Problem: https://www.hackerrank.com/challenges/marcs-cakewalk/problem
2+
# Score: 15
3+
Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a calorie count, and Marc can walk a distance to expend those calories. If Marc has eaten cupcakes so far, after eating a cupcake with calories he must walk at least miles to maintain his weight.
4+
5+
For example, if he eats cupcakes with calorie counts in the following order: , the miles he will need to walk are . This is not the minimum, though, so we need to test other orders of consumption. In this case, our minimum miles is calculated as .
6+
7+
Given the individual calorie counts for each of the cupcakes, determine the minimum number of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any order.
8+
9+
Function Description
10+
11+
Complete the marcsCakewalk function in the editor below. It should return a long integer that represents the minimum miles necessary.
12+
13+
marcsCakewalk has the following parameter(s):
14+
15+
calorie: an integer array that represents calorie count for each cupcake
16+
Input Format
17+
18+
The first line contains an integer , the number of cupcakes in .
19+
The second line contains space-separated integers .
20+
21+
Constraints
22+
23+
Output Format
24+
25+
Print a long integer denoting the minimum number of miles Marc must walk to maintain his weight.
26+
27+
Sample Input 0
28+
29+
3
30+
1 3 2
31+
Sample Output 0
32+
33+
11
34+
Explanation 0
35+
36+
Let's say the number of miles Marc must walk to maintain his weight is . He can minimize by eating the cupcakes in the following order:
37+
38+
Eat the cupcake with calories, so .
39+
Eat the cupcake with calories, so .
40+
Eat the cupcake with calories, so .
41+
We then print the final value of , which is , as our answer.
42+
43+
Sample Input 1
44+
45+
4
46+
7 4 9 6
47+
Sample Output 1
48+
49+
79
50+
'''
51+
52+
_ = input()
53+
calories = list(map(int, input().split()))
54+
calories = sorted(calories, reverse=True)
55+
ans = 0
56+
for index, cupcake in enumerate(calories):
57+
ans += 2**index*cupcake
58+
print(ans)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''# Problem: https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem
2+
# Score: 15
3+
Consider an array of integers, . We define the absolute difference between two elements, and (where ), to be the absolute value of .
4+
5+
Given an array of integers, find and print the minimum absolute difference between any two elements in the array. For example, given the array we can create pairs of numbers: and . The absolute differences for these pairs are , and . The minimum absolute difference is .
6+
7+
Function Description
8+
9+
Complete the minimumAbsoluteDifference function in the editor below. It should return an integer that represents the minimum absolute difference between any pair of elements.
10+
11+
minimumAbsoluteDifference has the following parameter(s):
12+
13+
n: an integer that represents the length of arr
14+
arr: an array of integers
15+
Input Format
16+
17+
The first line contains a single integer , the size of .
18+
The second line contains space-separated integers .
19+
20+
Constraints
21+
22+
Output Format
23+
24+
Print the minimum absolute difference between any two elements in the array.
25+
26+
Sample Input 0
27+
28+
3
29+
3 -7 0
30+
Sample Output 0
31+
32+
3
33+
Explanation 0
34+
35+
With integers in our array, we have three possible pairs: , , and . The absolute values of the differences between these pairs are as follows:
36+
37+
Notice that if we were to switch the order of the numbers in these pairs, the resulting absolute values would still be the same. The smallest of these possible absolute differences is .
38+
39+
Sample Input 1
40+
41+
10
42+
-59 -36 -13 1 -53 -92 -2 -96 -54 75
43+
Sample Output 1
44+
45+
1
46+
Explanation 1
47+
48+
The smallest absolute difference is .
49+
50+
Sample Input 2
51+
52+
5
53+
1 -3 71 68 17
54+
Sample Output 2
55+
56+
3
57+
Explanation 2
58+
59+
The minimum absolute difference is '''
60+
61+
_ = input()
62+
arr = sorted(map(int, input().split()))
63+
diff = 2*10**9
64+
for i in range(1, len(arr)):
65+
diff = min(diff, arr[i] - arr[i-1])
66+
print(diff)

Algorithms/Permuting Two Arrays.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''# Problem: https://www.hackerrank.com/challenges/two-arrays/problem
2+
# Score: 40
3+
Consider two -element arrays of integers, and . You want to permute them into some and such that the relation holds for all where . For example, if , , and , a valid satisfying our relation would be and , and .
4+
5+
You are given queries consisting of , , and . For each query, print YES on a new line if some permutation , satisfying the relation above exists. Otherwise, print NO.
6+
7+
Function Description
8+
9+
Complete the twoArrays function in the editor below. It should return a string, either YES or NO.
10+
11+
twoArrays has the following parameter(s):
12+
13+
k: an integer
14+
A: an array of integers
15+
B: an array of integers
16+
Input Format
17+
18+
The first line contains an integer , the number of queries.
19+
20+
The next sets of lines are as follows:
21+
22+
The first line contains two space-separated integers and , the size of both arrays and , and the relation variable.
23+
The second line contains space-separated integers .
24+
The third line contains space-separated integers .
25+
Constraints
26+
27+
Output Format
28+
29+
For each query, print YES on a new line if valid permutations exist. Otherwise, print NO.
30+
31+
Sample Input
32+
33+
2
34+
3 10
35+
2 1 3
36+
7 8 9
37+
4 5
38+
1 2 2 1
39+
3 3 3 4
40+
Sample Output
41+
42+
YES
43+
NO
44+
Explanation
45+
46+
We perform the following two queries:
47+
48+
, , and . We permute these into and so that the following statements are true:
49+
50+
Thus, we print YES on a new line.
51+
52+
, , and . To permute and into a valid and , we would need at least three numbers in to be greater than ; as this is not the case, we print NO on a new line.'''
53+
54+
n = int(input())
55+
for _ in range(n):
56+
__, k = map(int, input().split())
57+
a = sorted(list(map(int, input().split())))
58+
b = sorted(list(map(int, input().split())), reverse=True)
59+
if all([a[i] + b[i] >= k for i in range(len(a))]):
60+
print('YES')
61+
else:
62+
print('NO')

Python/Sales by Match.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
2+
3+
For example, there are socks with colors . There is one pair of color and one of color . There are three odd socks left, one of each color. The number of pairs is .
4+
5+
Function Description
6+
7+
Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.
8+
9+
sockMerchant has the following parameter(s):
10+
11+
n: the number of socks in the pile
12+
ar: the colors of each sock
13+
Input Format
14+
15+
The first line contains an integer , the number of socks represented in .
16+
The second line contains space-separated integers describing the colors of the socks in the pile.
17+
18+
Constraints
19+
Output Format
20+
21+
Return the total number of matching pairs of socks that Alex can sell.
22+
23+
Sample Input
24+
25+
9
26+
10 20 20 10 10 30 50 10 20
27+
Sample Output
28+
29+
3
30+
Explanation
31+
32+
sock.png
33+
34+
Alex can match three pairs of socks'''
35+
36+
#solution
37+
def sockMerchant(n, ar):
38+
return sum([ar.count(i)//2 for i in set(ar)])
39+
40+
if __name__ == '__main__':
41+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
42+
43+
n = int(input())
44+
45+
ar = list(map(int, input().rstrip().split()))
46+
47+
result = sockMerchant(n, ar)
48+
49+
fptr.write(str(result) + '\n')
50+
51+
fptr.close()

Python/Sales by Match.txt

Lines changed: 0 additions & 24 deletions
This file was deleted.

Python/Write_a_function.py.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
An extra day is added to the calendar almost every four years as February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.
3+
4+
In the Gregorian calendar, three conditions are used to identify leap years:
5+
6+
The year can be evenly divided by 4, is a leap year, unless:
7+
The year can be evenly divided by 100, it is NOT a leap year, unless:
8+
The year is also evenly divisible by 400. Then it is a leap year.
9+
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years. Source
10+
11+
Task
12+
13+
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.
14+
15+
Note that the code stub provided reads from STDIN and passes arguments to the is_leap function. It is only necessary to complete the is_leap function.
16+
17+
Input Format
18+
19+
Read , the year to test.
20+
21+
Constraints
22+
23+
24+
Output Format
25+
26+
The function must return a Boolean value (True/False). Output is handled by the provided code stub.
27+
28+
Sample Input 0
29+
30+
1990
31+
Sample Output 0
32+
33+
False
34+
Explanation 0
35+
36+
1990 is not a multiple of 4 hence it's not a leap year.'''
37+
38+
def is_leap(year):
39+
leapyear = False
40+
if (((year%4==0 and year%100!=0) or year%400==0) and year>=1900):
41+
leapyear=True
42+
return leapyear
43+
44+
year = int(input())
45+
print(is_leap(year))

0 commit comments

Comments
 (0)