Hackerrank’in 30 Days of Code içerisinde bulunan “Day 10: Binary Numbers” sorusunun açıklaması ve çözümü. Bu soruda verilen sayının binary gösteriminin içerdiği yan yana 1’lerin maksimum sayısını bulmanız isteniyor.
Today, we’re working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video!
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 ‘1s 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 is 1111101. In base 10, there are 5 and 1 consecutive ones in two groups. Print the maximum, 5.
Input Format
A single integer, n.
Output Format
Print a single base-10 integer that denotes the maximum number of consecutive 1’s in the binary representation of n.
Hackerrank’in 30 Days of Code içerisinde bulunan “Day 5: Loops” sorusunun açıklaması ve çözümü. Bu soruda loop (döngü) mantığını ve her bir döngüdeki değişkenleri nasıl kullanabileceğimizi öğrendik.
Hackerrank’in 30 Days of Code içerisinde bulunan “Day 4: Class vs. Instance” sorusunun açıklaması ve çözümü. Bu soruda sınıf ve field’lar üzerinde işlemler yapıyoruz.
In this challenge, we’re going to learn about the difference between a class and an instance; because this is an Object Oriented concept, it’s only enabled in certain languages. Check out the Tutorial tab for learning materials and an instructional video!
Task
Write a Person class with an instance variable, age, and a constructor that takes an integer, initialAge, as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as initialAge, the constructor should set age to 0 and print Age is not valid, setting age to 0.. In addition, you must write the following instance methods:
yearPasses() should increase the age instance variable by 1.
amIOld() should perform the following conditional actions:
If age less than 13, print You are young..
If age bigger or equal than 13 and age less than 18, print You are a teenager..
Otherwise, print You are old..
To help you learn by example and complete this challenge, much of the code is provided for you, but you’ll be writing everything in the future. The code that creates each instance of your Person class is in the main method. Don’t worry if you don’t understand it all quite yet!
Note: Do not remove or alter the stub code in the editor.
Input Format
Input is handled for you by the stub code in the editor.
The first line contains an integer, T (the number of test cases), and the T subsequent lines each contain an integer denoting the age of a Person instance.
Constraints
Output Format
Complete the method definitions provided in the editor so they meet the specifications outlined above; the code to test your work is already in the editor. If your methods are implemented correctly, each test case will print or lines (depending on whether or not a valid was passed to the constructor).
Sample Input
4
-1
10
16
18
Sample Output
Age is not valid, setting age to 0.
You are young.
You are young.
You are young.
You are a teenager.
You are a teenager.
You are old.
You are old.
You are old.
The extra line at the end of the output is supposed to be there and is trimmed before being compared against the test case’s expected output. If you’re failing this challenge, check your logic and review your print statements for spelling errors.
HackerRank içerisinde bulunan “Chocolate Feast” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir çikolata dükkanı için, belirli bir sayıda çikolata kabı getirdiğinizde belirli bir sayıda çikolata barı alabileceğiniz, belirli bir sayıda çikolata ile maksimum promosyonlar ile birlikte kaç adet çikolata yiyebileceğinizi hesaplamanız isteniyor.
Little Bobby loves chocolate. He frequently goes to his favorite 5&10 store, Penny Auntie, to buy them. They are having a promotion at Penny Auntie. If Bobby saves enough wrappers, he can turn them in for a free chocolate.
Example
n = 15
c = 3
m = 2
He has 15 to spend, bars cost 3, and he can turn in 2 wrappers to receive another bar. Initially, he buys 5 bars and has 5 wrappers after eating them. He turns in 4 of them, leaving him with 1, for 2 more bars. After eating those two, he has 3 wrappers, turns in 2 leaving him with 1 wrapper and his new bar. Once he eats that one, he has 2 wrappers and turns them in for another bar. After eating that one, he only has 1 wrapper, and his feast ends. Overall, he has eaten 5 + 2 + 1 + 1 = 9 bars.
Function Description
Complete the chocolateFeast function in the editor below.
chocolateFeast has the following parameter(s):
int n: Bobby’s initial amount of money
int c: the cost of a chocolate bar
int m: the number of wrappers he can turn in for a free bar
Returns
int: the number of chocolates Bobby can eat after taking full advantage of the promotion
Note: Little Bobby will always turn in his wrappers if he has enough to get a free chocolate.
HackerRank içerisinde bulunan “Repeated String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string ve n sayısı için, verilen string’i n uzunlukta olacak şekilde karakterlerini yazarak sonuç string’inde kaç tane ‘a’ karakteri olduğunu bulmanız isteniyor.
There is a string, s, of lowercase English letters that is repeated infinitely many times. Given an integer, n, find and print the number of letter a’s in the first n letters of the infinite string.
Example
s = ‘abcac’
n = 10
The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of a in the substring.
Function Description
Complete the repeatedString function in the editor below.
repeatedString has the following parameter(s):
s: a string to repeat
n: the number of characters to consider
Returns
int: the frequency of a in the substring
Input Format
The first line contains a single string, s.
The second line contains an integer, n.
Sample Input
Sample Input 0
aba
10
Sample Output 0
7
Explanation 0
The first n = 10 letters of the infinite string are abaabaabaa. Because there are 7 a’s, we return 7.
Sample Input 1
a
1000000000000
Sample Output 1
1000000000000
Explanation 1
Because all of the first n = 1000000000000 letters of the infinite string are a, we return 1000000000000 .
HackerRank içerisinde bulunan “Cycle Detection” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir linked list içerisinde döngüsel bir yapı olup olmadığını bulmanız isteniyor.
A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0.
Example
head refers to the list of nodes 1 — 2 — 3 — NULL
The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0.
head refers to the list of nodes 1 — 2 — 3 — 1 — NULL
There is a cycle where node 3 points back to node 1, so return 1.
Function Description
Complete the has_cycle function in the editor below.
It has the following parameter:
SinglyLinkedListNode pointer head: a reference to the head of the list
Returns
int:1 if there is a cycle or 0 if there is not
Note: If the list is empty, head will be null.
Input Format
The code stub reads from stdin and passes the appropriate argument to your function. The custom test cases format will not be described for this question due to its complexity. Expand the section for the main function and review the code if you would like to figure out how to create a custom case.
HackerRank içerisinde bulunan “Sherlock and Squares” sorusunun açıklaması ve çözümü. Bu soruda verilen iki sayı arasındaki tam kare olan sayıların adetini bulmanız istenir.
Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range.
Note: A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25
Example
a = 24
b = 49
There are three square integers in the range: 25, 36 and 49. Return 3.
Function Description
Complete the squares function in the editor below. It should return an integer representing the number of square integers in the inclusive range from a to b.
squares has the following parameter(s):
int a: the lower range boundary
int b: the upper range boundary
Returns
int: the number of square integers in the range.
Input Format
The first line contains q, the number of test cases. Each of the next q lines contains two space-separated integers, a and b, the starting and ending integers in the ranges.
HackerRank içerisinde bulunan “Viral Advertising” sorusunun açıklaması ve çözümü. HackerLand Enterprise firması yeni bir reklam stratejisi üzerinde çalışıyor. İlk günde bu reklamı 5 kişiye gösteriyorlar sosyal medyada. Ve bu kişi sayısının yarısı 3’er arkadaşı ile paylaşıyor bu reklamı. Size verilen n gün sonra bu reklamı kaç kişi gördüğünü bulmanız isteniyor.
HackerLand Enterprise is adopting a new viral advertising strategy. When they launch a new product, they advertise it to exactly 5 people on social media.
On the first day, half of those 5 people (i.e., 5 / 2 = 2) like the advertisement and each shares it with 3 of their friends. At the beginning of the second day, (5 / 2 * 3 = 2 *3 = 6) people receive the advertisement.
Each day, floor(recipients / 2) of the recipients like the advertisement and will share it with 3 friends on the following day. Assuming nobody receives the advertisement twice, determine how many people have liked the ad by the end of a given day, beginning with launch day as day 1.
Example
n = 5.
Day Shared Liked Cumulative
1 5 2 2
2 6 3 5
3 9 4 9
4 12 6 15
5 18 9 24
The progression is shown above. The cumulative number of likes on the 5th day is 24.
Function Description
Complete the viralAdvertising function in the editor below.
HackerRank içerisinde bulunan “Mars Exploration” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir “SOS” string’inin orijinalinden kaç adet farklı karakter barındırdığını bulmanız isteniyor.
A space explorer’s ship crashed on Mars! They send a series of SOS messages to Earth for help.
Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, s, determine how many letters of the SOS message have been changed by radiation.
Example
s = “SOSTOT”
The original message was SOSSOS. Two of the message’s characters were changed in transit.
Function Description
Complete the marsExploration function in the editor below.
marsExploration has the following parameter(s):
string s: the string as received on Earth
Returns
int: the number of letters changed during transmission
Sample Input 0
SOSSPSSQSSOR
Sample Output 0
3
Explanation 0
= SOSSPSSQSSOR, and signal length s = |12|. They sent 4 SOS messages (i.e.: ).
Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR
Difference: X X X
Sample Input 1
SOSSOT
Sample Output 1
1
Explanation 1
s = SOSSOT, and signal length 6. They sent 2 SOS messages (i.e.: ).