Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
humorhacker1 authored May 6, 2024
1 parent b9e60cf commit 75504b2
Show file tree
Hide file tree
Showing 37 changed files with 3,572 additions and 1 deletion.
49 changes: 49 additions & 0 deletions 30 Days of Code (Solutions)/Day 0: Hello, Wold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''
Objective:
In this challenge, we review some basic concepts that will get you started with this series.
You will need to use the same (or similar) syntax to read input and write output in challenges throughout HackerRank.
Check out the Tutorial tab for learning materials and an instructional video!
https://www.hackerrank.com/challenges/30-hello-world/tutorial
Task:
To complete this challenge,
you must save a line of input from stdin to a variable,
print Hello, World. on a single line,
and finally print the value of your variable on a second line.
You've got this!
Note: The instructions are Java-based,
but we support submissions in many popular languages.
You can switch languages using the drop-down menu above your editor,
and the inputString variable may be written differently depending
on the best-practice conventions of your submission language.
Input Format:
A single line of text denoting inputString (the variable whose contents must be printed).
Output Format:
Print Hello, World. on the first line, and the contents of inputString on the second line.
Sample Input:
Welcome to 30 Days of Code!
Sample Output:
Hello, World.
Welcome to 30 Days of Code!
Explanation:
On the first line, we print the string literal Hello, World..
On the second line, we print the contents of the variable which, for this sample case,
happens to be Welcome to 30 Days of Code!.
If you do not print the variable's contents to stdout, you will not pass the hidden test case.
'''

# Solution:
# Read a full line of input from stdin and save it to our dynamically typed variable, input_string.
input_string = input()

# Print a string literal saying "Hello, World." to stdout.
print('Hello, World.')

# TODO: Write a line of code here that prints the contents of input_string to stdout.
print(input_string)
107 changes: 107 additions & 0 deletions 30 Days of Code (Solutions)/Day 10: Binary Numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'''
Objective:
Today, we're working with binary numbers.
Check out the Tutorial tab for learning materials and an instructional video!
https://www.hackerrank.com/challenges/30-binary-numbers/tutorial
Task:
Given a base-10 integer, n, convert it to binary (base-2).
Then find and print the base-10 integer
denoting the maximum number of consecutive 1's in n's binary representation.
When working with different bases, it is common to show the base as a subscript.
Example:
n = 125
The binary representation of 125 (base-10) is 1111101 (base-2).
In base 10, there are 5 and 1 consecutive ones in two groups.
Print the maximum, 5.
Input Format:
A single integer, n.
Constraints:
1 <= n <= 10^6
Output Format:
Print a single base-10 integer that denotes the maximum number of
consecutive 1's in the binary representation of n.
Sample Input 1:
5
Sample Output 1:
1
Sample Input 2:
13
Sample Output 2:
2
Explanation:
Sample Case 1:
The binary representation of 5(base-10) is 101(base-2),
so the maximum number of consecutive 1's is 1.
Sample Case 2:
The binary representation of 13(base-10) is 1101(base-2),
so the maximum number of consecutive 1's is 2.
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input().strip())
'''

# Solution:
#!/bin/python3

import math # import the math module to use the math.factorial() function
import os # import the os module to interact with the operating system
import random # import the random module to generate random numbers
import re # import the re module to work with regular expressions
import sys # import the sys module to work with the Python runtime environment

if __name__ == '__main__': # if the script is being run directly
n = int(input().strip()) # read an integer from input and strip leading/trailing whitespaces
binary = bin(n)[2:] # convert the integer to binary
count = 0 # initialize a variable to store the count of consecutive 1's
max_count = 0 # initialize a variable to store the maximum count of consecutive 1's
for i in binary: # iterate through the binary representation
if i == '1': # if the current digit is '1'
count += 1 # increment the count by 1
max_count = max(count, max_count) # update the maximum count
else: # if the current digit is '0'
count = 0 # reset the count to 0
print(max_count) # print the maximum count of consecutive 1's

# Time complexity: O(log(n))
# Space complexity: O(1)

# Explanation:
# 1. Convert the integer to binary.
# 2. Iterate through the binary number.
# 3. If the current digit is '1', increment the count by 1.
# 4. If the current digit is '0', reset the count to 0.
# 5. Keep track of the maximum count of consecutive 1's.
# 6. Print the maximum count of consecutive 1's.
# 7. The time complexity is O(log(n)) as we are iterating through the binary representation of the number.
# 8. The space complexity is O(1) as we are using a constant amount of space for variables.
# 9. The code reads an integer from input, converts it to binary, and calculates the maximum number of consecutive 1's in the binary representation.
# 10. It then prints the maximum number of consecutive 1's.
# 11. The solution uses bitwise operations to convert the integer to binary and count the consecutive 1's efficiently.
# 12. The code is concise and efficient, providing the correct output for the given input.
# 13. The solution is correct and optimal, meeting the constraints of the problem.
# 14. The code is well-structured and easy to understand, making it suitable for submission.
# 15. The solution is efficient and handles large inputs effectively.
# 16. The code is clean and concise, providing a clear and efficient solution to the problem.

161 changes: 161 additions & 0 deletions 30 Days of Code (Solutions)/Day 11: 2D Arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
'''
Objective:
Today, we are building on our knowledge of arrays by adding another dimension.
Check out the Tutorial tab for learning materials and an instructional video.
https://www.hackerrank.com/challenges/30-2d-arrays/tutorial
Context:
Given a 6x6 2D Array, A:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in A to be a subset of values
with indices falling in this pattern in A's graphical representation:
a b c
d
e f g
There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass' values.
Task:
Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.
Example:
In the array shown above, the maximum hourglass sum is 7 for the hourglass in the top left corner.
Input Format:
There are 6 lines of input,
where each line contains 6 space-separated integers that describe the 2D Array A.
Constraints:
-9 <= A[i][j] <= 9
0 <= i,j <= 5
Output Format:
Print the maximum hourglass sum in A.
Sample Input:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output:
19
Explanation:
A contains the following hourglasses:
1 1 1 1 1 0 1 0 0 0 0 0
1 0 0 0
1 1 1 1 1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0
1 1 0 0
0 0 2 0 2 4 2 4 4 4 4 0
1 1 1 1 1 0 1 0 0 0 0 0
0 2 4 4
0 0 0 0 0 2 0 2 0 2 0 0
0 0 2 0 2 4 2 4 4 4 4 0
0 0 2 0
0 0 1 0 1 2 1 2 4 2 4 0
The hourglass with the maximum sum (19) is:
2 4 4
2
1 2 4
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
arr = []
for _ in range(6):
arr.append(list(map(int, input().rstrip().split())))
'''

# Solution:
#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__': # if the script is run directly

arr = [] # initialize an empty list

for _ in range(6): # iterate over a range of 6
arr.append(list(map(int, input().rstrip().split()))) # append a list of integers to arr

hourglass_sums = [] # initialize an empty list to store the sums of hourglasses
for i in range(4): # iterate over a range of 4
for j in range(4): # iterate over a range of 4
hourglass_sum = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]
# calculate the sum of the hourglass
hourglass_sums.append(hourglass_sum) # append the sum to hourglass_sums

max_hourglass_sum = max(hourglass_sums) # find the maximum sum of the hourglasses
print(max_hourglass_sum) # print the maximum sum of the hourglasses


# Explanation:
'''
The code you provided is a solution to a problem that involves printing
the first 10 multiples of a given integer `n`.
Let's break down the code further:
1. The code imports several modules: `math`, `os`, `random`, `re`, and `sys`.
These modules provide various functionalities related to mathematics, operating system operations,
random number generation, regular expressions, and system-specific parameters and functions.
2. The `if __name__ == '__main__':`
condition checks if the current module is being run as the main program.
This allows the code inside this block to be executed only when the module is run directly,
and not when it is imported as a module in another program.
3. The code prompts the user to enter an integer value for `n` using the `input()` function.
The `int()` function is then used to convert the user input to an integer,
and the `strip()` function is used to remove any leading or trailing spaces from the input.
4. The `for` loop is used to iterate over the range of numbers from 1 to 10 (inclusive).
The `range()` function is a built-in function that generates a sequence of numbers.
In this case, it generates the numbers 1, 2, 3, ..., 10.
5. Inside the loop, the variable `result` is calculated by multiplying `n`
with the current value of `i`.
6. The `print()` function is used to display the result of the multiplication
in the format `n x i = result`. The `f-string` syntax is used to format the string
by embedding the values of `n`, `i`, and `result` into the string.
7. After the loop finishes executing, the program ends.
This code effectively prints the first 10 multiples of the given integer `n` in the desired format.
'''
Loading

0 comments on commit 75504b2

Please sign in to comment.