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

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

► Hackerrank 30 Days of Code Çözümleri – Day 19: Interfaces: https://www.hackerrank.com/challenges/30-interfaces/problem

► Problem açıklaması:

Objective

Today, we’re learning about Interfaces. Check out the Tutorial tab for learning materials and an instructional video!

The AdvancedArithmetic interface and the method declaration for the abstract divisorSum(n) method are provided for you in the editor below.

Complete the implementation of Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum(n) method must return the sum of all divisors of n.

Example

n = 25

The divisors of 25 are 1, 5, 25. Their sum is 31.

n = 20

The divisors of 20 are 1, 2, 4, 5, 10, 20 and their sum is 42.

Input Format

A single line with an integer, n.

Output Format

You are not responsible for printing anything to stdout. The locked template code in the editor below will call your code and print the necessary output.

Sample Input

6

Sample Output

12

# Hackerrank “30 Days of Code” Çözümleri – Day 18: Queues and Stacks

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 18: Queues and Stacks” sorusunun açıklaması ve çözümü. Bu soruda queue ve stack yapısına göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 18: Queues and Stacks: https://www.hackerrank.com/challenges/30-queues-stacks/problem

► Problem açıklaması:

Welcome to Day 18! Today we’re learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video!

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backwards and forwards. Can you determine if a given string, s, is a palindrome?

To solve this challenge, we must first take each character in s, enqueue it in a queue, and also push that same character onto a stack. Once that’s done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means s isn’t a palindrome).

Write the following declarations and implementations:

Two instance variables: one for your stack, and one for your queue.

A void pushCharacter(char ch) method that pushes a character onto a stack.

A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.

A char popCharacter() method that pops and returns the character at the top of the stack instance variable.

A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.

Input Format

You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string s. It then calls the methods specified above to pass each character to your instance variables.

Constraints

is composed of lowercase English letters.

Output Format

You are not responsible for printing any output to stdout.

Sample Input

racecar

Sample Output

The word, racecar, is a palindrome.

# Hackerrank “30 Days of Code” Çözümleri – Day 17: More Exceptions

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 17: More Exceptions” sorusunun açıklaması ve çözümü. Bu soruda exception kavramına göz atmaya devam ettik.

► Hackerrank 30 Days of Code Çözümleri – Day 17: More Exceptions: https://www.hackerrank.com/challenges/30-more-exceptions/problem

► Problem açıklaması:

Objective

Yesterday’s challenge taught you to manage exceptional situations by using try and catch blocks. In today’s challenge, you will practice throwing and propagating an exception. Check out the Tutorial tab for learning materials and an instructional video.

Write a Calculator class with a single method: int power(int,int). The power method takes two integers, n and p, as parameters and returns the integer result of n^p. If either n or p is negative, then the method must throw an exception with the message: n and p should be non-negative.

Note: Do not use an access modifier (e.g.: public) in the declaration for your Calculator class.

Input Format

Input from stdin is handled for you by the locked stub code in your editor. The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers that denote n and p, respectively.

Constraints

No Test Case will result in overflow for correctly written code.

Output Format

Output to stdout is handled for you by the locked stub code in your editor. There are T lines of output, where each line contains the result of n^p as calculated by your Calculator class’ power method.

Sample Input

4

3 5

2 4

-1 -2

-1 3

Sample Output

243

16

n and p should be non-negative

n and p should be non-negative

# Hackerrank “30 Days of Code” Çözümleri – Day 16: Exceptions – String to Integer

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 16: Exceptions – String to Integer” sorusunun açıklaması ve çözümü. Bu soruda try catch ile exception handling kısmında kısaca baktık.

► Hackerrank 30 Days of Code Çözümleri – Day 16: Exceptions – String to Integer: https://www.hackerrank.com/challenges/30-exceptions-string-to-integer/problem

► Problem açıklaması:

Objective

Today, we’re getting started with Exceptions by learning how to parse an integer from a string and print a custom error message. Check out the Tutorial tab for learning materials and an instructional video!

Read a string, S, and print its integer value; if S cannot be converted to an integer, print Bad String.

Note: You must use the String-to-Integer and exception handling constructs built into your submission language. If you attempt to use loops/conditional statements, you will get a 0 score.

Input Format

A single string, S.

Output Format

Print the parsed integer value of S, or Bad String if S cannot be converted to an integer.

Sample Input 0

3

Sample Output 0

3

Sample Input 1

za

Sample Output 1

Explanation

Sample Case 0 contains an integer, so it should not raise an exception when we attempt to convert it to an integer. Thus, we print the 3.

Sample Case 1 does not contain any integers, so an attempt to convert it to an integer will raise an exception. Thus, our exception handler prints Bad String.

# Hackerrank “30 Days of Code” Çözümleri – Day 15: Linked List

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 15: Linked List” sorusunun açıklaması ve çözümü. Bu soruda linked list veri yapısına ve içerisindeki node’larda iterasyon yapılmasına göz atıyoruz.

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

► Problem açıklaması:

Objective

Today we will work with a Linked List. 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 the list).

A Node insert function is also declared in your editor. It has two parameters: a pointer, head, pointing to the first node of a linked list, and an integer, data, that must be added to the end of the list as a new Node object.

Complete the insert function in your editor so that it creates a new Node (pass data as the Node constructor argument) and inserts it at the tail of the linked list referenced by the head parameter. Once the new node is added, return the reference to the head node.

Note: The head argument is null for an empty list.

Input Format

The first line contains T, the number of elements to insert.

Each of the next T lines contains an integer to insert at the end of the list.

Output Format

Sample Input

STDIN Function

—– ——–

4 T = 4

2 first data = 2

3

4

1 fourth data = 1

Sample Output

2 3 4 1

Explanation

T = 4, so your method will insert 4 nodes into an initially empty list. First the code returns a new node that contains the data value 2 as the head of the list. Then create and insert nodes 3, 4, and a at the tail of the list.

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

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 14: Scope” sorusunun açıklaması ve çözümü. Bu soruda scope kavramına göz atmaya çalıştık.

► Hackerrank 30 Days of Code Çözümleri – Day 14: Scope: https://www.hackerrank.com/challenges/30-scope/problem

► Problem açıklaması:

Objective

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

The absolute difference between two integers, a and b, is written as |a – b|. The maximum absolute difference between two integers in a set of positive integers, elements, is the largest absolute difference between any two integers in _elements.

The Difference class is started for you in the editor. It has a private integer array (elements) for storing N non-negative integers, and a public integer (maximumDifference) for storing the maximum absolute difference.

Complete the Difference class by writing the following:

A class constructor that takes an array of integers as a parameter and saves it to the instance variable. A computeDifference method that finds the maximum absolute difference between any numbers in and stores it in the instance variable.

Input Format

You are not responsible for reading any input from stdin. The locked Solution class in the editor reads in lines of input. The first line contains N, the size of the elements array. The second line has space-separated integers that describe the array.

Output Format

You are not responsible for printing any output; the Solution class will print the value of the maximumDifference instance variable.

Sample Input

STDIN Function

—– ——–

3 __elements[] size N = 3

1 2 5 __elements = [1, 2, 5]

Sample Output

4

# Hackerrank “30 Days of Code” Çözümleri – Day 13: Abstract Classes

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 13: Abstract Classes” sorusunun açıklaması ve çözümü. Bu soruda abstract sınıflara ve base anahtar sözcüğüne göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 13: Abstract Classes: https://www.hackerrank.com/challenges/30-abstract-classes/problem

► Problem açıklaması:

Objective

Today, we will extend what we learned yesterday about Inheritance to Abstract Classes. Because this is a very specific object oriented concept, submissions are limited to the few languages that use this construct. Check out the Tutorial tab for learning materials and an instructional video.

Given a Book class and a Solution class, write a MyBook class that does the following:

Inherits from Book

Has a parameterized constructor taking these 3 parameters:

string title

string author

int price

Implements the Book class’ abstract display() method so it prints these 3 lines:

Title , a space, and then the current instance’s title.

Author , a space, and then the current instance’s author.

Price , a space, and then the current instance’s price.

Note: Because these classes are being written in the same file, you must not use an access modifier (e.g.: public) when declaring MyBook or your code will not execute.

Input Format

You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object.

Output Format

Title: \$title

Author: \$author

Price: \$price

Note: The \$ is prepended to variable names to indicate they are placeholders for variables.

Sample Input

The following input from stdin is handled by the locked stub code in your editor:

The Alchemist

Paulo Coelho

248

Sample Output

The following output is printed by your display() method:

Title: The Alchemist

Author: Paulo Coelho

Price: 248