Hanwool Codes RSS Tag Admin Write Guestbook
Categories (45)
2023-01-26 06:11:27

Write a function | HackerRank

 

Write a function | HackerRank

Write a function to check if the given year is leap or not

www.hackerrank.com

 

Goal

 

This task is to return true if a give year is a leap year, otherwise return false

 

 

Solution

 

There are three main conditions to check leap year:

1. It can be evenly divided by 4 - leap year

2. It can be evenly divided by 100, it is NOT a leap year, unless it is evenly disivible by 400 - leap year

3. Otherwise it is not a leap year.

 

These conditions can be shown in this code.

# Write a function

def is_leap(year):
    leap = False
    
    # Write your logic here
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                leap = True
            else : 
                leap = False
        else:  
            leap = True
        
    else:
        leap = False
    
    return leap

year = int(input())
print(is_leap(year))
2023-01-24 06:01:01

For Loop | HackerRank

 

For Loop | HackerRank

Learn how to use for loop and print the output as per the given conditions

www.hackerrank.com

 

Goal

 

Print results according to two positive input values by using for loop statement.

 

 

Solution

 

To solve easily, I create vector string array included 1-9 numbers. If you use vector array, you can print numbers easily.

 

// For Loop
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;

int main() {
    // Complete the code.
    int a,b;
    cin >> a;
    cin >> b;
    
    vector<string> numbers {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    
    for(int i = a; i <= b; i++){
        
        if(i>9){
            if( i % 2 == 0){
                cout << "even" << endl;
            }
            else{
                cout << "odd" << endl;
            }
        }
        
        else{
            
            cout << numbers[i] << endl;
        }
    }
    
    return 0;
}
2023-01-24 05:55:31

https://www.hackerrank.com/challenges/c-tutorial-conditional-if-else/problem

 

Conditional Statements | HackerRank

Practice using chained conditional statements.

www.hackerrank.com

 

Goal

 

 

Solution

 

You just need to create condtion statements for each condition like below.

 

// Conditional Statements

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);



int main()
{
    string n_temp;
    getline(cin, n_temp);

    int n = stoi(ltrim(rtrim(n_temp)));

    // Write your code here
    if(n > 9){
        printf("Greater than 9");
    }
    else if(n == 1){
        printf("one");
    }
    else if(n == 2){
        printf("two");
    }
    else if(n == 3){
        printf("three");
    }
    else if(n == 4){
        printf("four");
    }
    else if(n == 5){
        printf("five");
    }
    else if(n == 6){
        printf("six");
    }
    else if(n == 7){
        printf("seven");
    }
    else if(n == 8){
        printf("eight");
    }
    else { // n == 9
        printf("nine");
    }

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}
2023-01-22 18:00:34

Basic Data Types | HackerRank

 

Basic Data Types | HackerRank

Learn about the basic data types in C++. Take the given input and print them.

www.hackerrank.com

 

Goal

1. Read space-seprated values : int, long, char, float and double, respectively

2. Print each element on a new line in the same order it was received as input. + correct decimal points of floating and double points.

 

 

Solution

Follow HackerRank explanation of c++ data types and their format specifiers.

// Basic Data Types

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
    long b;
    char c;
    float d;
    double e;

    scanf("%d %ld %c %f %lf", &a, &b, &c, &d, &e);
    printf("%d\n", a);
    printf("%ld\n", b);
    printf("%c\n", c);
    printf("%f\n", d);
    printf("%lf\n", e);
    return 0;
}
2023-01-21 18:00:25

Loops | HackerRank

 

Loops | HackerRank

Practice using "for" and "while" loops in Python.

www.hackerrank.com

 

Goal

 

Read integer value n, and print i² for all non-negative integer i < n.

 

 

Solution

If you know for statement in python, it woul be very easy task.

# Loops

if __name__ == '__main__':
    n = int(input())
    
    for i in range(n):
        print(i * i)
2023-01-20 18:00:20

Python: Division | HackerRank

 

Python: Division | HackerRank

Division using __future__ module.

www.hackerrank.com

 

Goal

 

1. Read two integers each line.

2. print (a//b) in first line.

3. print (a/b) in second line.

 

 

 

Solution

 

Use input() function to read two integer values. But don't forget to convert input return value into integer value. 

# Python: Division

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    print(a // b)
    print(a / b)
2023-01-19 17:06:46

Arithmetic Operators | HackerRank

 

Arithmetic Operators | HackerRank

Addition, subtraction and multiplication.

www.hackerrank.com

 

Goal

 

1. Read two integers from STDIN.

2. Solve tasks to use arithmetic operators. 

 

 

Solution

 

You can read values by using input() function. The important thing is to convert input() value into integer type because Input() function returns string value from input. 

 

After read two integer values, you can calculate vaules for each task.

 

# Arithmetic Operators

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    sum = a + b
    difference = a - b
    product = a * b
    
    print(sum)
    print(difference)
    print(product)
2023-01-19 16:57:00

Python If-Else | HackerRank

 

Python If-Else | HackerRank

Practice using if-else conditional statements

www.hackerrank.com

 

Goal

 

You need to print "Weird" if the number is weird in under condition. Otherweise, print "Not Weird". 

 

 

Solution

You can follow what conditions say, then it would be easy.

 

# Python If-Else

import math
import os
import random
import re
import sys


if __name__ == '__main__':
    n = int(input().strip())
    
    if (n % 2) !=0 : 
        print('Weird')
    else:
        if (n >=2 and n <=5):
            print('Not Weird')
        
        elif (n >=6 and n <=20):
            print('Weird')
            
        else:
            print('Not Weird')

 

2023-01-19 16:49:23

Say "Hello, World!" With Python | HackerRank

 

Say "Hello, World!" With Python | HackerRank

Get started with Python by printing to stdout.

www.hackerrank.com

 

Goal

 

Print "Hello, World!" to stdout. 

 

Solution

 

You can use print function to print the specificed message. 

 

# Say "Hello, World!" With Python

if __name__ == '__main__':
    print("Hello, World!")
2023-01-18 06:13:37

Input and Output | HackerRank

 

Input and Output | HackerRank

Learn to take in the input and print the output. Take three number as input and print their sum as output.

www.hackerrank.com

 

Goal

 

Read 3 numbers from stdin and print their sum to stdout

 

 

Solution

 

This challenge is to practice reading input from stdin(standard input stream) and printing out to stdout(standard output stream).

 

// Input and Output

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int a, b,c ;
    int result = 0;
    cin >> a >> b >> c;
    result = a + b + c;
    cout << result << endl;
    return 0;
}

 

 

 



Hanwool Codes. Designed by 코딩재개발.