Hanwool Codes RSS Tag Admin Write Guestbook
Python Learning (28)
2023-01-28 05:21:45

List Comprehensions | HackerRank

 

List Comprehensions | HackerRank

You will learn about list comprehensions.

www.hackerrank.com

 

Goal

 

Print a list of all possible coordinates given by (i, j, k) on a 3D grid where the sum of i + j + k is not equal to n.

 

 

Solution

You can simply make a condition statement {(i + j + k) != n} to append all (i,j,k) combinations into list and print the list. 

 

There are two simple ways to solve this problem. You can use simply multiple loops, but you can use easily loop comprehensions. 

 

1) The for statement

 

# List Comprehensions

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())

    results = []

    for i in range( x + 1):
        for j in range( y + 1):
            for k in range(z + 1):
                if (i + j + k) != n:
                    results.append([i, j, k])


    print(results)

 

2) List Comprehension

# List Comprehensions

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())

    results = [[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n]

    print(results)
2023-01-27 04:59:45

Print Function | HackerRank

 

Print Function | HackerRank

Learn to use print as a function

www.hackerrank.com

 

Goal

 

Print the list of integers from 1 through give n as a string, without spaces

 

 

Solution

 

To solve this problem, you can use for statement and str class to convert integer value to string object. 

 

# Print Function

if __name__ == '__main__':
    n = int(input())
    
    number = ""
    for i in range(1, n+1, 1):
        number += str(i)
        
    print(int(number))
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-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!")


Hanwool Codes. Designed by 코딩재개발.