# Hackerrank “30 Days of Code” Çözümleri – Day 27: Testing

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 27: Testing” sorusunun açıklaması ve çözümü. Bu soruda baz test metodları yazarak “unit testing” kavramına giriş yaptık.

► Hackerrank 30 Days of Code Çözümleri – Day 27: Testing: https://www.hackerrank.com/challenges/30-testing/problem

► Problem açıklaması:

This problem is about unit testing.

Your company needs a function that meets the following requirements:

For a given array of n integers, the function returns the index of the element with the minimum value in the array. If there is more than one element with the minimum value, it returns the smallest one.

If an empty array is passed to the function, it raises an exception. A colleague has written this method. The implementation in Python is listed below. Implementations in other languages can be found in the code template.

def minimum_index(seq): if len(seq) == 0: raise ValueError(“Cannot get the minimum value index from an empty sequence”) min_idx = 0 for i in range(1, len(seq)): if a[i] less a[min_idx]: min_idx = i return min_idx

A coworker has prepared functions that will perform the tests and validate return values. Finish the implementation of 3 classes to provide data and expected results for the tests.

Complete the following methods.

In the class TestDataEmptyArray:

get_array() returns an empty array

In the class TestDataUniqueValues:

get_array() returns an array of size at least 2 with all unique elements

get_expected_result() returns the expected minimum value index for this array

In the class TestDataExactlyTwoDifferentMinimums:

get_array() returns an array where the minimum value occurs at exactly 2 indices

get_expected_result() returns the expected index

Take a look at the code template to see the exact implementation of functions that your colleague already implemented.

Note: The arrays are indexed from 0.

# Hackerrank “30 Days of Code” Çözümleri – Day 26: Nested Logic

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 26: Nested Logic” sorusunun açıklaması ve çözümü. Bu soruda iç içe koşul ifadelerine göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 26: Nested Logic: https://www.hackerrank.com/challenges/30-nested-logic/problem

► Problem açıklaması:

Objective

Today’s challenge puts your understanding of nested conditional statements to the test. You already have the knowledge to complete this challenge, but check out the Tutorial tab for a video on testing.

Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows:

If the book is returned on or before the expected return date, no fine will be charged (i.e.: . fine = 0)

If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, . fine = 15 Hackos * (the number of days late)

If the book is returned after the expected return month but still within the same calendar year as the expected return date, the . fine = 500 Hackos * (the number of months late)

If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos.

Input Format The first line contains space-separated integers denoting the respective , , and on which the book was actually returned. The second line contains space-separated integers denoting the respective , , and on which the book was expected to be returned (due date).

Output Format

Print a single integer denoting the library fine for the book received as input.

Sample Input

STDIN Function —– ——–

9 6 2015 day = 9, month = 6, year = 2015 (date returned)

6 6 2015 day = 6, month = 6, year = 2015 (date due)

Sample Output

45

# Hackerrank “30 Days of Code” Çözümleri – Day 25: Running Time and Complexity

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 25: Running Time and Complexity” sorusunun açıklaması ve çözümü. Bu soruda size verilen sayının bir asal sayı olup olmadığını, time complexity kavramını anlatarak bulmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 25: Running Time and Complexity: https://www.hackerrank.com/challenges/30-running-time-and-complexity/problem

► Problem açıklaması:

Objective

Today we will learn about running time, also known as time complexity. Check out the Tutorial tab for learning materials and an instructional video.

A prime is a natural number greater than that has no positive divisors other than 1 and itself. Given a number, n, determine and print whether it is Prime or Not prime.

Note: If possible, try to come up with a squareRoot(n) primality algorithm, or see what sort of optimizations you come up with for an O(n) algorithm. Be sure to check out the Editorial after submitting your code.

Input Format

The first line contains an integer, , the number of test cases. Each of the subsequent lines contains an integer, , to be tested for primality.

Output Format

For each test case, print whether n is Prime or Not prime on a new line.

Sample Input

3

12

5

7

Sample Output

Not prime

Prime

Prime

# Hackerrank “30 Days of Code” Çözümleri – Day 24: More Linked Lists

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 24: More Linked Lists” sorusunun açıklaması ve çözümü. Bu soruda sıralı bir linked list içerisindeki duplicate node’ları silmemiz isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 24: More Linked Lists: https://www.hackerrank.com/challenges/30-linked-list-deletion/problem

► Problem açıklaması: Objective Check out the Tutorial tab for learning materials and an instructional video!

A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in a list).

A removeDuplicates function is declared in your editor, which takes a pointer to the head node of a linked list as a parameter. Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list.

Note: The head pointer may be null, indicating that the list is empty. Be sure to reset your next pointer when performing deletions to avoid breaking the list.

Input Format

You do not need to read any input from stdin. The following input is handled by the locked stub code and passed to the removeDuplicates function:

The first line contains an integer, N, the number of nodes to be inserted.

The N subsequent lines each contain an integer describing the data value of a node being inserted at the list’s tail.

Constraints

The data elements of the linked list argument will always be in non-decreasing order. Output Format

Your removeDuplicates function should return the head of the updated linked list. The locked stub code in your editor will print the returned list to stdout.

Sample Input

6

1

2

2

3

3

4

Sample Output

1 2 3 4

Explanation

N=6, and our non-decreasing list is {1, 2, 2, 3, 3, 4}. The values 2 and 3 both occur twice in the list, so we remove the two duplicate nodes. We then return our updated (ascending) list, which is {1, 2, 3, 4}.

# Hackerrank “30 Days of Code” Çözümleri – Day 23: BST Level-Order Traversal

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 23: BST Level-Order Traversal” sorusunun açıklaması ve çözümü. Bu soruda bir binary search tree içerisinde level-order traversal işleminin nasıl yapılabileceğini öğrendik.

► Hackerrank 30 Days of Code Çözümleri – Day 23: BST Level-Order Traversal: https://www.hackerrank.com/challenges/30-binary-trees/problem

► Problem açıklaması:

Objective

Today, we’re going further with Binary Search Trees. Check out the Tutorial tab for learning materials and an instructional video!

A level-order traversal, also known as a breadth-first search, visits each level of a tree’s nodes from left to right, top to bottom. You are given a pointer, root, pointing to the root of a binary search tree. Complete the levelOrder function provided in your editor so that it prints the level-order traversal of the binary search tree.

Hint: You’ll find a queue helpful in completing this challenge.

Function Description

Complete the levelOrder function in the editor below.

levelOrder has the following parameter:

– Node pointer root: a reference to the root of the tree

Prints

– Print node.data items as space-separated line of integers. No return value is expected.

Input Format

The locked stub code in your editor reads the following inputs and assembles them into a BST: The first line contains an integer, T (the number of test cases).

The T subsequent lines each contain an integer, data, denoting the value of an element that must be added to the BST.

Output Format

Print the data value of each node in the tree’s level-order traversal as a single line of N space-separated integers.

Sample Input

6

3

5

4

7

2

1

Sample Output

3 2 5 1 4 7

Explanation

The input forms the following binary search tree:

https://s3.amazonaws.com/hr-challenge-images/17176/1461696188-8eddd12300-BST.png

We traverse each level of the tree from the root downward, and we process the nodes at each level from left to right. The resulting level-order traversal is 3-2-5-1-4-7, and we print these data values as a single line of space-separated integers.

# Hackerrank “30 Days of Code” Çözümleri – Day 22: Binary Search Trees

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 22: Binary Search Trees” sorusunun açıklaması ve çözümü. Bu soruda bir binary search tree‘nin yüksekliğini hesapladık.

► Hackerrank 30 Days of Code Çözümleri – Day 22: Binary Search Trees: https://www.hackerrank.com/challenges/30-binary-search-trees/problem

► Problem açıklaması:

Objective

Today, we’re working with Binary Search Trees (BSTs). Check out the Tutorial tab for learning materials and an instructional video!

The height of a binary search tree is the number of edges between the tree’s root and its furthest leaf. You are given a pointer, root, pointing to the root of a binary search tree. Complete the getHeight function provided in your editor so that it returns the height of the binary search tree.

Input Format

The locked stub code in your editor reads the following inputs and assembles them into a binary search tree: The first line contains an integer, n, denoting the number of nodes in the tree.

Each of the n subsequent lines contains an integer, data, denoting the value of an element that must be added to the BST.

Output Format

The locked stub code in your editor will print the integer returned by your getHeight function denoting the height of the BST.

Sample Input

7

3

5

2

1

4

6

7

Sample Output

3

Explanation

The input forms the following BST:

https://s3.amazonaws.com/hr-challenge-images/17175/1459894869-6bb53ce6eb-BST.png

The longest root-to-leaf path is shown below:

https://s3.amazonaws.com/hr-challenge-images/17175/1459895368-4955f9ce74-LongestRTL.png

There are 4 nodes in this path that are connected by 3 edges, meaning our BST’s height = 3. Thus, we print 3 as our answer.

# Hackerrank “30 Days of Code” Çözümleri – Day 21: Generics

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 21: Generics” sorusunun açıklaması ve çözümü. Bu soruda generics kavramına göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 21: Generics: https://www.hackerrank.com/challenges/30-generics/problem

► Problem açıklaması:

Objective

Today we’re discussing Generics; be aware that not all languages support this construct, so fewer languages are enabled for this challenge. Check out the Tutorial tab for learning materials and an instructional video!

Write a single generic function named printArray; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector). The locked Solution class in your editor tests your function.

Note: You must use generics to solve this challenge. Do not write overloaded functions.

Input Format

The locked Solution class in your editor will pass different types of arrays to your printArray function.

Constraints

You must have exactly 1 function named printArray.

Output Format

Your printArray function should print each element of its generic array parameter on a new line.

# Hackerrank “30 Days of Code” Çözümleri – Day 20: Sorting

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 20: Sorting” sorusunun açıklaması ve çözümü. Bu soruda bubble sort kavramına göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 20: Sorting: https://www.hackerrank.com/challenges/30-sorting/problem

► Problem açıklaması:

Objective

Today, we’re discussing a simple sorting algorithm called Bubble Sort. Check out the Tutorial tab for learning materials and an instructional video!

Given an array, a, of size n distinct elements, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following 3 lines:

Array is sorted in numSwaps swaps.

where numSways is the number of swaps that took place.

First Element: firstElement

where firstElement is the first element in the sorted array.

Last Element: lastElement

where lastElement is the last element in the sorted array.

Hint: To complete this challenge, you will need to add a variable that keeps a running tally of all swaps that occur during execution.

Example a = [4, 3, 1, 2]

original a: 4 3 1 2

round 1 a: 3 1 2 4 swaps this round: 3

round 2 a: 1 2 3 4 swaps this round: 2

round 3 a: 1 2 3 4 swaps this round: 0

In the first round, the is swapped at each of the comparisons, ending in the last position. In the second round, the is swapped at of the comparisons. Finally, in the third round, no swaps are made so the iterations stop. The output is the following:

Array is sorted in 5 swaps.

First Element: 1

Last Element: 4

Input Format

The first line contains an integer, , the number of elements in array . The second line contains space-separated integers that describe .

Output Format

Print the following three lines of output:

Array is sorted in numSwaps swaps.

where is the number of swaps that took place.

First Element: firstElement

where is the first element in the sorted array.

Last Element: lastElement

where is the last element in the sorted array.

Sample Input 0

3

1 2 3

Sample Output 0

Array is sorted in 0 swaps.

First Element: 1

Last Element: 3

Explanation 0

The array is already sorted, so swaps take place and we print the necessary lines of output shown above.

Sample Input 1

3

3 2 1

Sample Output 1

Array is sorted in 3 swaps.

First Element: 1

Last Element: 3