Hanwool Codes RSS Tag Admin Write Guestbook
software Engineering (42)
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;
}

 

 

 

2023-01-18 06:06:40

Say "Hello, World!" With C++ | HackerRank

 

Say "Hello, World!" With C++ | HackerRank

Practice printing to stdout.

www.hackerrank.com

 

Goal

 

Solution

 

This is simple challenge to print "Hello, World!" to stdout. We can use easily an object of class iostream. "std::cout."

 

// Say "Hello, World!" With C++

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

int main() {

    cout << "Hello, World!" << endl;
    return 0;
}

 

2023-01-10 07:40:22

Today, I will show how to convert m4a file into mp3 file in python.

My test environment is OS : Windows 10 and Python version : 3.7.6

 

Convert m4a file into mp3 file in Python

 

Firstly, you need to install python library "pydub" 

 

GitHub - jiaaro/pydub: Manipulate audio with a simple and easy high level interface

Manipulate audio with a simple and easy high level interface - GitHub - jiaaro/pydub: Manipulate audio with a simple and easy high level interface

github.com

 

But to use pydub library, you need to install ffmpeg or libav.

 

FFmpeg

Converting video and audio has never been so easy. $ ffmpeg -i input.mp4 output.avi     News July 22nd, 2022, FFmpeg 5.1 "Riemann" FFmpeg 5.1 "Riemann", a new major release, is now available! Some of the highlights: add ipfs/ipns protocol support dialogu

www.ffmpeg.org

 

GitHub - libav/libav: Libav github mirror, clone of git://git.libav.org/libav

Libav github mirror, clone of git://git.libav.org/libav - GitHub - libav/libav: Libav github mirror, clone of git://git.libav.org/libav

github.com

 

You can install ffmpeg-downloader package with below command.

pip install ffmpeg-downloader
ffdl install --add-path

The --add-path option adds the installed FFmpeg folder to the user's system path. In Windows, you need to add your PATH to use ffmpeg library. After add PATH, please reopen python interpretor or other script editor to run ffmpeg properly.

 

If you have other OS, you can check ffmpeg homepage or pydub github page. They explain details how to install ffmpeg in your OS.

 

I provide you sample m4a file to test your script. You can see more free m4a files here.

sample3.m4a
1.66MB

 

 

The sample source code is below.

from pydub import AudioSegment

song = AudioSegment.from_file("sample3.m4a")
song.export("sample3.mp3", format="mp3")

 

If you look at pydub github page, you can convert m4a file into any audio format file.

 

 

 

If you have any question, please write a comment!

'software Engineering > python' 카테고리의 다른 글

How to convert HEIC file into PNG in Python  (0) 2023.01.09
2023-01-09 03:40:30

Today, I will show how to convert HEIC file into PNG in python.

HEIC file is High Efficiency Image File Format. It is for storing individual images and image sequences. 

But HEIC format file does not accept my image editors or blog. So today I want to show how to convert HEIC file into PNG in Python.

 

Convert HEIC into PNG in Python

 

Firstly, you need to install python library "pillow_heif".

Additionall, I also use Pillow library to save heic format data into png format.

Also, I provide you sample heic file to test your script. You can see more free heic files here.

sample1.heic
0.28MB

The sample source code is below.

from PIL import Image
import pillow_heif

image_file  = "sample1.heic"

im = pillow_heif.read_heif(image_file)
image = Image.frombytes(
    mode = im.mode,
    size = im.size,
    data = im.data
)

image.save("convert_png.png", format("png"))

 

As you can see save format, you can also save HEIC file into JPEG, TIFF and others. It is always up to you and your project.

 

Reference

● bigcat88 (no date) Bigcat88/PILLOW_HEIF: Python library to work with HEIF files and an add-on for pillow., GitHub. Available at: https://github.com/bigcat88/pillow_heif (Accessed: January 8, 2023). 

● Sample heic files download (no date) Sample HEIC Files Download -  Get Examples Instantly. Available at: https://filesamples.com/formats/heic (Accessed: January 8, 2023). 

 

 

If you have any question, please write a comment!

'software Engineering > python' 카테고리의 다른 글

How to convert m4a file into mp3 file in Python  (0) 2023.01.10


Hanwool Codes. Designed by 코딩재개발.