Light Blue Pointer
본문 바로가기
Coding Test

[프로그래머스][Lv.0]a와 b 출력하기

by 개발바닥곰발바닥!!! 2023. 10. 25.

문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/181951

문제 

문제 설명

정수 a와 b가 주어집니다. 각 수를 입력받아 입출력 예와 같은 형식으로 출력하는 코드를 작성해 보세요.


제한사항

  • 100,000 ≤ a, b ≤ 100,000

입출력 예

입력 #1

4 5

출력 #1

a = 4 b = 5

a, b = map(int, input().strip().split(' '))
print(a)

제출 코드

a, b = map(int, input().strip().split(' '))
print(f"a = {a}\nb = {b}")

a, b = map(int, input().strip().split(' '))
print("a =",a) 
print("b =",b)

a, b = map(int, input().strip().split(' '))
print("a =", str(a))
print("b =", str(b))

공부한 것

⭐lambda

⭐How to merge string in Python?

We can perform string concatenation using following ways:

  1. Using + operator.
  2. Using join() method.
  3. Using % operator.
  4. Using format() function.
  5. Using f-string (Literal String Interpolation)

Python String strip() Method

Remove spaces at the beginning and at the end of the string:

txt = "     banana     "x = txt.strip()print("of all fruits", x, "is my favorite")

banana

txt = ",,,,,rrttgg.....banana....rrr"

x = txt.strip(",.grt")

print(x)

banana

 

Python map() Function

The map() function executes a specified function for each item in an iterable. The item is sent to the function as a parameter.

 

map(function, iterables)

 

Parameter Description

function Required. The function to execute for each item
iterable Required. A sequence, collection or an iterator object. You can send as many iterables as you like, just make sure the function has one parameter for each iterable.

 

Example 1

def myfunc(n):
  return len(n)

x = map(myfunc, ('apple', 'banana', 'cherry'))

<map object at 0x056D44F0>

[5, 6, 6]

 

Example 2

def myfunc(a, b):
  return a + b

x = map(myfunc, ('apple', 'banana', 'cherry'), ('orange', 'lemon', 'pineapple'))

<map object at 0x034244F0>
['appleorange', 'bananalemon', 'cherrypineapple']

 

 

Python String split() Method

Split a string into a list where each word is a list item:

 

string.split(separator, maxsplit)

 

Parameter Description

separator Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator
maxsplit Optional. Specifies how many splits o do. Default value is -1, which is "all occurrences"

 

Example 1: Split the string, using comma, followed by a space, as a separator

txt = "welcome to the jungle"

x = txt.split()

print(x)

['welcome', 'to', 'the', 'jungle']