Tag Archives: hackerrank

Hackerrank “30 Days of Code” Çözümleri – Day 10: Binary Numbers

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.

► Hackerrank 30 Days of Code Çözümleri – Day 10: Binary Numbers: https://www.hackerrank.com/challenges/30-binary-numbers/problem

► Problem açıklaması:

Objective

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.

Sample Input 1

5

Sample Output 1

1

Sample Input 2

13

Sample Output 2

2

Hackerrank “30 Days of Code” Çözümleri – Day 9: Recursion 3

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 9: Recursion 3” sorusunun açıklaması ve çözümü. Bu soruda rekürsif fonksiyonlara giriş yapıyoruz. Derler ki; “Rekürsif fonksiyonları anlamak için önce rekürsif fonksiyonları anlamanız gerekir“.

► Hackerrank 30 Days of Code Çözümleri – Day 9: Recursion 3: https://www.hackerrank.com/challenges/30-recursion/problem

► Problem açıklaması:

Objective

Today, we are learning about an algorithmic concept called recursion. Check out the Tutorial tab for learning materials and an instructional video.

Recursive Method for Calculating Factorial Function Description Complete the factorial function in the editor below. Be sure to use recursion.

factorial has the following paramter:

int n: an integer

Returns

int: the factorial of n

Note: If you fail to use recursion or fail to name your recursive function factorial or Factorial, you will get a score of 0.

Input Format

A single integer, n (the argument to pass to factorial).

Constraints

– 2 lessEqual n lessEqual 12

– Your submission must contain a recursive function named factorial.

Sample Input

3

Sample Output

6

Explanation

Consider the following steps. After the recursive calls from step 1 to 3, results are accumulated from step 3 to 1.

factorial(3) = 3 x factorial(2) = 3 x 2 = 6

factorial(2) = 2 x factorial(1) = 2 x 1 = 2

factorial(1) = 1

Hackerrank “30 Days of Code” Çözümleri – Day 8: Dictionaries and Maps

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 8: Dictionaries and Maps” sorusunun açıklaması ve çözümü. Bu soruda dictionary ve hash table mantığına göz atıyoruz.

► Hackerrank 30 Days of Code Çözümleri – Day 8: Dictionaries and Maps: https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem

► Problem açıklaması:

Objective

Today, we’re learning about Key-Value pair mappings using a Map or Dictionary data structure. Check out the Tutorial tab for learning materials and an instructional video!

Task

Given n names and phone numbers, assemble a phone book that maps friends’ names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.

Note: Your phone book should be a Dictionary/Map/HashMap data structure.

Input Format

The first line contains an integer, n, denoting the number of entries in the phone book. Each of the n subsequent lines describes an entry in the form of 2 space-separated values on a single line. The first value is a friend’s name, and the second value is an 8-digit phone number.

After the n lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a name to look up, and you must continue reading lines until there is no more input.

Note: Names consist of lowercase English alphabetic letters and are first names only.

Output Format

On a new line for each query, print Not found if the name has no corresponding entry in the phone book; otherwise, print the full name and phoneNumber in the format name=phoneNumber.

Sample Input

3

sam 99912222

tom 11122222

harry 12299933

sam

edward

harry

Sample Output

sam=99912222

Not found

harry=12299933

Hackerrank “30 Days of Code” Çözümleri – Day 7: Arrays

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 7: Arrays” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir liste içerisinde, uzunluğundan ilk index’ine kadar elemanları yazdırmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 7: Arrays: https://www.hackerrank.com/challenges/30-arrays/problem

► Problem açıklaması:

Objective

Today, we will learn about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video.

Task

Given an array, A, of N integers, print A’s elements in reverse order as a single line of space-separated numbers.

Example

A=[1, 2, 3, 4]

Print 4 3 2 1. Each integer is separated by one space.

Input Format

The first line contains an integer, N (the size of our array). The second line contains N space-separated integers that describe array A’s elements.

Output Format

Print the elements of array A in reverse order as a single line of space-separated numbers.

Sample Input

4

1 4 3 2

Sample Output

2 3 4 1

Hackerrank “30 Days of Code” Çözümleri – Day 6: Let’s Review

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 6: Let’s Review” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string‘in çift ve tek index’teki karakterlerini ayrı ayrı yazdırmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 6: Let’s Review: https://www.hackerrank.com/challenges/30-review-loop/problem

► Problem açıklaması:

Objective

Today we will expand our knowledge of strings, combining it with what we have already learned about loops. Check out the Tutorial tab for learning materials and an instructional video.

Task

Given a string, S, of length N that is indexed from 0 to N – 1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Note: 0 is considered to be an even index.

Example

s = adbecf

Print abc def

Input Format

The first line contains an integer, T (the number of test cases).

Each line i of the T subsequent lines contain a string, S.

Output Format

For each String (where ), print ‘s even-indexed characters, followed by a space, followed by ‘s odd-indexed characters.

Sample Input

2

Hacker

Rank

Sample Output

Hce akr

Rn ak

Hackerrank “30 Days of Code” Çözümleri – Day 5: Loops

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 30 Days of Code Çözümleri – Day 5: Loops: https://www.hackerrank.com/challenges/30-loops/problem

► Problem açıklaması:

Objective

In this challenge, we will use loops to do some math. Check out the Tutorial tab to learn more.

Task

Given an integer, n, print its first 10 multiples. Each multiple n x i should be printed on a new line in the form: n x i = result.

Example

The printout should look like this:

3 x 1 = 3

3 x 2 = 6

3 x 3 = 9

3 x 4 = 12

3 x 5 = 15

3 x 6 = 18

3 x 7 = 21

3 x 8 = 24

3 x 9 = 27

3 x 10 = 30

Input Format

A single integer, n.

Constraints

Output Format

Print 10 lines of output; each line i (where 1 lessEqual i lessEqual 10) contains the result of n x i in the form:

n x i = result.

Sample Input

2

Sample Output

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

Hackerrank “30 Days of Code” Çözümleri – Day 4: Class vs. Instance

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.

► Hackerrank 30 Days of Code Çözümleri – Day 4: Class vs. Instance: https://www.hackerrank.com/challenges/30-class-vs-instance/problem

► Problem açıklaması:

Objective

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 “30 Days of Code” Çözümleri – Day 3: Intro to Conditional Statements

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 3: Intro to Conditional Statements” sorusunun açıklaması ve çözümü. Bu soruda karşılaştırma operatörlerine giriş yapıyoruz.

► Hackerrank 30 Days of Code Çözümleri – Day 3: Intro to Conditional Statements: https://www.hackerrank.com/challenges/30-conditional-statements/problem

► Problem açıklaması:

Objective

In this challenge, we learn about conditional statements. Check out the Tutorial tab for learning materials and an instructional video.

Task

Given an integer, n, perform the following conditional actions:

If n is odd, print Weird

If n is even and in the inclusive range of 2 to 5, print Not Weird

If n is even and in the inclusive range of 6 to 20, print Weird

If n is even and greater than 20, print Not Weird

Complete the stub code provided in your editor to print whether or not n is weird.

Input Format

A single line containing a positive integer, n.

Output Format

Print Weird if the number is weird; otherwise, print Not Weird.

Sample Input 0

3

Sample Output 0

Weird

Sample Input 1

24

Sample Output 1

Not Weird

Explanation

Sample Case 0: n = 3

n is odd and odd numbers are weird, so we print Weird.

Sample Case 1: n = 24

n biggerThan 20 and is even, so it is not weird. Thus, we print Not Weird.

Hackerrank “30 Days of Code” Çözümleri – Day 2: Operators

Hackerrank‘in 30 Days of Code içerisinde bulunan “Day 2: Operators” sorusunun açıklaması ve çözümü. Bu soruda size verilen yemek ücreti, bahşiş ve vergi yüzdeleri ile birlikte toplamda ne kadar para ödemeniz gerektiğinizi bulmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 2: Operators: https://www.hackerrank.com/challenges/30-operators/problem

► Problem açıklaması:

Objective

In this challenge, you will work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video.

Task

Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal’s total cost. Round the result to the nearest integer.

Example

mealCost = 100

tipPercent = 15

taxPercent = 8

A tip of 15% * 100 = 15, and the taxes are 8% * 100 = 8. Print the value 123 and return from the function.

Function Description

Complete the solve function in the editor below.

solve has the following parameters:

int meal_cost: the cost of food before tip and tax

int tip_percent: the tip percentage

int tax_percent: the tax percentage

Returns The function returns nothing. Print the calculated value, rounded to the nearest integer.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result.

Input Format

There are 3 lines of numeric input:

The first line has a double, mealCost (the cost of the meal before tax and tip).

The second line has an integer, tipPercent (the percentage of mealCost being added as tip).

The third line has an integer, taxPercent (the percentage of mealCost being added as tax).

Sample Input

12.00

20

8

Sample Output

15

Hackerrank “30 Days of Code” Çözümleri – Day 1: Data Types

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 1: Data Types” sorusunun açıklaması ve çözümü. Bu soruda sizin konsol‘dan aldığınız int, double ve string tipinde değişkenlerini “+” operatörü kullanarak konsola yazdırmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 1: Data Types: https://www.hackerrank.com/challenges/30-data-types/problem

► Problem açıklaması:

Objective

Today, we’re discussing data types. Check out the Tutorial tab for learning materials and an instructional video!

Task

Complete the code in the editor below. The variables , , and are already declared and initialized for you. You must:

1. Declare 3 variables: one of type int, one of type double, and one of type String.

2. Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your 3 variables.

3. Use the + operator to perform the following operations:

– Print the sum of i plus your int variable on a new line.

– Print the sum of d plus your double variable to a scale of one decimal place on a new line.

– Concatenate s with the string you read as input and print the result on a new line.

Note: If you are using a language that doesn’t support using + for string concatenation (e.g.: C), you can just print one variable immediately following the other on the same line. The string provided in your editor must be printed first, immediately followed by the string you read as input.

Input Format

The first line contains an integer that you must sum with i.

The second line contains a double that you must sum with d.

The third line contains a string that you must concatenate with s.

Output Format

Print the sum of both integers on the first line, the sum of both doubles (scaled to 1 decimal place) on the second line, and then the two concatenated strings on the third line.

Sample Input

12

4.0

is the best place to learn and practice coding!

Sample Output

16

8.0

HackerRank is the best place to learn and practice coding!

Explanation

When we sum the integers 4 and 12, we get the integer 16.

When we sum the floating-point numbers 4.0 and 4.0, we get 8.0.

When we concatenate HackerRank with is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!.

You will not pass this challenge if you attempt to assign the Sample Case values to your variables instead of following the instructions above and reading input from stdin.