[Algorithm] Algorithm Test

William Parker·2022년 11월 17일
post-thumbnail

Algorithm time attack was conducted using the programmer's coding test website.

1. Get a discount at a clothing store

  • Problem description
    The clothing store offers a 5% discount for purchases of $100,000 or more, 10% for purchases of $300,000 or more, and 20% discount for purchases of $500,000 or more.
    Complete the solution function to return the amount to be paid when the price of the purchased clothes is given.

  • Restrictions
    10 ≤ price ≤ 1,000,000
    The price is given in increments of 10 won (1's place is 0).
    Returns an integer with decimal places rounded down.

  • My answer code

import math


def solution(price):
    answer = 0
    if 100000<=price<300000:
        price= price*0.95
    elif 300000<=price<500000:
        price= price*0.9
    elif 500000<=price:
        price= price*0.8
    else:
        price=price

    answer=math.trunc(price)
    return answer

There was no difficulty in solving the problem at the basic level, and there were restrictions in the middle, but I felt that it was confirmed.

2. 369 Games

  • Problem statement
    I am playing the 369 game with my shy friends. The 369 game is a game in which numbers are entered one by one from 1, and the numbers entering 3, 6, and 9 are clapped as many times as the number of 3, 6, and 9 instead of the number. Complete the solution function to return the number of claps that Mushroom should clap when the number order that Mushroom should say is given as a parameter.
  • Restrictions
    1 ≤ order ≤ 1,000,000
  • My answer code
def solution(order):
    answer=0
    
    num1 = [int(x) for x in str(order)]
    
    for i in num1:
        if i==3:
            answer+=1
        elif i==6:
            answer+=1
        elif i==9:
            answer+=1
    
    return answer

3. log-in succeed?

  • Problem description
    I'm trying to log in to Programmers. When an array id_pw containing the ID and password entered by the mother and a 2-dimensional array db containing the information of the members are given, complete the solution function to return messages according to login success or failure as follows.
    If there is member information that matches both ID and password, "login" is returned.
    Returns “fail” if there is no member with matching ID when login fails, and “wrong pw” if there is no member with matching ID but matching password.

  • Restrictions
    Member IDs are strings.
    Member IDs consist of lowercase letters and numbers only.
    Members' passwords are strings of numbers.
    Members can have the same password, but not the same ID.
    The length of id_pw is 2.
    The elements of id_pw and db are in the form of [ID, password].
    1 ≤ ID length ≤ 15
    1 ≤ password length ≤ 6
    1 ≤ length of db ≤ 10
    The length of the elements of db is 2.

  • My answer code

def solution(id_pw, db):
    answer=""
    
    for i in range(len(db)):
    
            if db[i][0] == id_pw[0]:
                if db[i][1] == id_pw[1] :
                    answer +="login"
                elif db[i][1] != id_pw[1]:
                    answer += "wrong pw"
            else:
                answer+="fail"
    if "login" in answer:
        answer = "login"
    elif "wrong pw" in answer:
        answer = "wrong pw"
    else:
        answer = "fail"
    
    return answer
  • Another person code
def solution(id_pw, db):
    dic = dict(db)
    if dic.get(id_pw[0],-1) == id_pw[1]:
        return "login"
    elif dic.get(id_pw[0],-1) == -1:
        return "fail"
    else:
        return "wrong pw"

While solving this problem, even though I logged in at the return part, the test failed because the fail was continuously called together. When I debugged it, login and fail were added together. After thinking about how to solve it, I wrote that if fail and login are together, login is possible. However, it wasn't the code I wanted, so I first solved the problem, then analyzed the code written by someone else and found a difference. As a result, I tried to keep putting values in the answer when I kept returning. However, if you return immediately without having to do that, only the value is output. It was a really simple problem, but I couldn't come up with this, and I thought I should study more about how to use functions.

Reference
1. https://programmers.co.kr/

profile
Developer who does not give up and keeps on going.

0개의 댓글