์ฝ”๋”ฉ ๋ฌธ์ œ #2๐Ÿงฉ

18082 ๋‹จ์–ด pythonchallengebeginnersjavascript
๋ฌธ์ œ ํ•ด๊ฒฐ์ž ์—ฌ๋Ÿฌ๋ถ„๊ป˜ ์•ˆ๋ถ€๋ฅผ ๋ฌป๊ฒ ์Šต๋‹ˆ๋‹ค!๐Ÿค“
๋‚ด๊ฐ€ ์ด๋ฏธ ๋งํ•œ ๋ฐ”์™€ ๊ฐ™์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์€ ํ•˜๋‚˜์˜ ๊ทผ์œก๊ณผ ๊ฐ™์•„์„œ ๊ฐœ๋ฐœ์ž๋กœ์„œ ์ž์ฃผ ์—ฐ์Šตํ•ด์•ผ ํ–ฅ์ƒ์‹œํ‚ค๊ณ  ์„ฑ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
2ํšŒ์—์„œ ๋‚˜๋Š” ๋‚ด๊ฐ€ ๊ฐ€์žฅ ์ข‹์•„ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์ดํŠธ์—์„œ ํ•ด๊ฒฐํ•˜๊ธฐ ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ๊ณจ๋ž๋‹ค.

์นดํƒˆ๋กœ๊ทธ

  • Can you get the loop ?
  • Morse Code
  • Rectangle into Squares
  • Meeting
  • Playing with digits
  • ๊ทธ ๊ณ ๋ฆฌ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ฒ ์†Œ?


    ๋ถ€ํ„ฐCodewars
    ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    ์ฒด์ธ ํ…Œ์ด๋ธ”๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.์ด ๋ชฉ๋ก์€ ํ•ญ์ƒ ๊ผฌ๋ฆฌ์™€ ์ˆœํ™˜์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
    ๋„ˆ์˜ ๋ชฉํ‘œ๋Š” ์ˆœํ™˜์˜ ๊ธธ์ด๋ฅผ ํ™•์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ๊ทธ๋ฆผ์—์„œ ๊ผฌ๋ฆฌ์˜ ํฌ๊ธฐ๋Š” 3์ด๊ณ  ๊ณ ๋ฆฌ์˜ ํฌ๊ธฐ๋Š” 11์ด๋‹ค.

    "next"์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
    ์ฃผ์˜: ๋…ธ๋“œ๋ฅผ ๋ณ€์ดํ•˜์ง€ ๋งˆ์„ธ์š”!
    ๋‚ด ์†”๋ฃจ์…˜:
    function loop_size(node){
      let turtle = node;
      let rabbit = node;
    
      /* We need the turtle and the rabbit to start at the same 
      place. (The rabbit being faster will catch up the turtle at 
      some point) */
      do {
        turtle = turtle.getNext();
        rabbit = rabbit.getNext().getNext();
      } while (turtle !== rabbit)
    
      /* The rabbit goes for a run and we know that ends when he 
      reaches the turtle position again. */
      let counter = 0;
      do {
        rabbit = rabbit.getNext();
        counter++;
      } while (turtle !== rabbit)
    
        return counter;
    } 
    
    

    ๋ชจ์Šค ๋ถ€ํ˜ธ


    ๋ถ€ํ„ฐCodewars
    ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    ์ด ์นดํƒ€์—์„œ, ๋„ˆ๋Š” ๊ฐ„๋‹จํ•œ ๋ชจ์Šค ๋””์ฝ”๋”๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.๋ชจ์Šค ์ฝ”๋“œ๋Š” ํ˜„์žฌ ์ฃผ๋กœ ์Œ์„ฑ๊ณผ ๋””์ง€ํ„ธ ๋ฐ์ดํ„ฐ ํ†ต์‹  ์ฑ„๋„๋กœ ๋Œ€์ฒด๋˜๊ณ  ์žˆ์ง€๋งŒ, ์—ฌ์ „ํžˆ ์„ธ๊ณ„ ๊ฐ์ง€์˜ ์ผ๋ถ€ ์‘์šฉ์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค.
    ๋ชจ์Šค ์ฝ”๋“œ๋Š” ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ผ๋ จ์˜ '์ ' ๊ณผ '๋Œ€์‹œ' ๋กœ ์ธ์ฝ”๋”ฉํ•œ๋‹ค.์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž A๋Š” ยท๋กœ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค.โˆ’, ๋ฌธ์ž Q ์ธ์ฝ”๋”ฉโˆ’โˆ’ยทโˆ’, ์ˆซ์ž 1์ด ยท๋กœ ์ธ์ฝ”๋”ฉ๋จโˆ’โˆ’โˆ’โˆ’. ๋ชจ์Šค ์ฝ”๋“œ๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์ „ํ†ต์ ์œผ๋กœ ๋Œ€๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.์ •๋ณด๋ฅผ ๋ชจ์Šค ์ฝ”๋“œ๋กœ ์“ธ ๋•Œ, ํ•œ ๊ฐœ์˜ ๋นˆ์นธ์€ ๋ฌธ์ž ์ฝ”๋“œ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๊ณ , ์„ธ ๊ฐœ์˜ ๋นˆ์นธ์€ ๋‹จ์–ด๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
    ์ฃผ์˜: ์ฝ”๋“œ์˜ ์•ž๋’ค ์ถ”๊ฐ€ ๋นˆ์นธ์€ ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ฌด์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    ์•ŒํŒŒ๋ฒณ, ์ˆซ์ž์™€ ์ผ๋ถ€ ๋ฌธ์žฅ๋ถ€ํ˜ธ๋ฅผ ์ œ์™ธํ•˜๊ณ  ํŠน์ˆ˜ํ•œ ์„œ๋น„์Šค ์ฝ”๋“œ๋„ ์žˆ๋‹ค. ๊ทธ ์ค‘์—์„œ ๊ฐ€์žฅ ์•…๋ช… ๋†’์€ ๊ฒƒ์€ ๊ตญ์ œ ์กฐ๋‚œ ์‹ ํ˜ธ SOS(์ตœ์ดˆ๋กœ ํƒ€์ดํƒ€๋‹‰ ํ˜ธ์— ์˜ํ•ด ๋ฐœํ‘œ), ์ฝ”๋“œ๋Š” ยทยทยทยทยทโˆ’โˆ’โˆ’ยทยทยท. ์ด ํŠน์ˆ˜ ์ฝ”๋“œ๋“ค์€ ๋ณดํ†ต ๋‹จ๋… ๋ฌธ์ž๋กœ ์ „์†ก๋˜๋Š” ๋‹จ์ผ ํŠน์ˆ˜ ๋ฌธ์ž๋กœ ์—ฌ๊ฒจ์ง„๋‹ค.
    ๋ชจ์–ด์Šค ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜๊ณ  ๋””์ฝ”๋”ฉ๋œ ์ธ๊ฐ„์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ๋˜๋Œ๋ ค ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋‹น์‹ ์˜ ์ž„๋ฌด์ž…๋‹ˆ๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    decodeMorse('.... . -.--   .--- ..- -.. .')
    #should return "HEY JUDE"
    
    ์ฐธ๊ณ : ์ธ์ฝ”๋”ฉ์„ ์œ„ํ•ด ASCII ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.๋ฐ - ๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.
    ๋ชจ์Šค ์ฝ”๋“œ๋Š” ์‚ฌ์ „์œผ๋กœ ๋ฏธ๋ฆฌ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๋งˆ์Œ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.
    JavaScript/TypeScript: MORSE ์ฝ”๋“œ ['--']
    ๋‚ด ์†”๋ฃจ์…˜:
    const decodeMorse = (morseCode) => {
      const response = [];
    
      const words = morseCode.trim().split('   ');
    
      for (const word of words) {
        const wordArr = [];
        for (const letter of word.split(' ')) {
          wordArr.push(MORSE_CODE[letter]);
        }
        response.push(wordArr.join(''))
      }
    
      return response.join(' ');
    
    }
    

    ์ง์‚ฌ๊ฐํ˜•์ด ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋ณ€ํ•˜๋‹ค


    ๋ถ€ํ„ฐCodewars
    ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ฃผ์–ด์ง„ '์ง„' ์‚ฌ๊ฐํ˜•์„ ์–ด๋–ป๊ฒŒ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ์ž๋ฅด๋Š”์ง€ ๋ณด์—ฌ ์ค€๋‹ค. ('์ง„' ์‚ฌ๊ฐํ˜•์€ ๋‘ ์ฐจ์›์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.)

    ๋„ˆ๋Š” ์ด ๊ทธ๋ฆผ์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฒˆ์—ญํ•  ์ˆ˜ ์žˆ๋‹ˆ?
    ๋„ˆ๋Š” ๋‘ ๊ฐ€์ง€ ์ฐจ์›์„ ์–ป๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค
  • ์ •์ˆ˜ ๊ธธ์ด(lng์ด๋ผ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜)
  • ์ •์ˆ˜ ๋„ˆ๋น„ (wdth๋ผ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜)
  • ๊ฐ ๋ธ”๋ก์˜ ํฌ๊ธฐ๋ฅผ ํฌํ•จํ•˜๋Š” ์ปฌ๋ ‰์…˜์ด๋‚˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค(์–ธ์–ด, Shell bash, PowerShell, Pascal ๋ฐ Fortran์— ๋”ฐ๋ผ ๋‹ค๋ฆ„).
    ์˜ˆ:
      sqInRect(5, 3) should return "3 2 1 1"
      sqInRect(3, 5) should return "3 2 1 1"
    
    ๋‚ด ์†”๋ฃจ์…˜:
    function sqInRect(lng, wdth){
      console.log(lng, wdth);
    
      if (lng === wdth) { return null; }
    
      let lngx = lng;
      let wdthx = wdth;
    
      let area = lng * wdth;
      let result = [];
    
    
    
      while (area > 0) {
        if (lngx > wdthx) {
          area -= Math.pow(wdthx, 2);
          result.push(wdthx);
          lngx =  lngx - wdthx;
        } else {
          area -= Math.pow(lngx, 2);
          result.push(lngx);
          wdthx = wdthx - lngx;
        }
      }
    
      return result;  
    }
    

    ๋ฉดํšŒ


    ๋ถ€ํ„ฐCodewars
    ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    ์กด์€ ๋ช‡๋ช‡ ์นœ๊ตฌ๋“ค์„ ์ดˆ๋Œ€ํ–ˆ๋‹ค.๊ทธ์˜ ๋ช…๋‹จ์€:
    s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill";
    
    ๋„ˆ๋Š” ์ด๋Ÿฐ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ˆ
  • ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค
  • .
  • ์„ฑ์”จ์— ๋”ฐ๋ผ ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  • ์„ฑ์”จ๊ฐ€ ๊ฐ™์œผ๋ฉด ์ด๋ฆ„์— ๋”ฐ๋ผ ์ˆœ์„œ๋ฅผ ์ •ํ•œ๋‹ค.์†๋‹˜์˜ ์„ฑ๊ณผ ์ด๋ฆ„์ด ๊ฒฐ๊ณผ์— ๋‚˜ํƒ€๋‚˜ ๊ด„ํ˜ธ ์‚ฌ์ด๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
    ๋”ฐ๋ผ์„œ ๊ธฐ๋Šฅ ํšŒ์˜์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    "(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)"
    
    ๊ฐ™์€ ์„ฑ์„ ๊ฐ€์ง„ ๋‘ ๊ฐ€์กฑ ์ค‘ ๋‘ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„๋„ ๊ฐ™์„ ์ˆ˜ ์žˆ๋‹ค.
    ๋‚ด ์†”๋ฃจ์…˜(Python):
    def meeting(s):
        result = ""
    
        # Convert string to list
        names_list = s.upper().split(";")
    
        # Create a dictionnary and bind a list of first names to every last names
        names_dic = {}
        for name in names_list:
            first_name = name.split(":")[0]
            last_name = name.split(":")[1]
    
            if last_name in names_dic: names_dic[last_name].append(first_name)
            else: names_dic[last_name] = [first_name]
    
        # Sort and add every entry to the result string
        for key in sorted(names_dic):
            for first_name in sorted(names_dic[key]):
                result = result + f"({key}, {first_name})"
    
        return result
    

    ๋””์ง€ํ„ธ ๊ฒŒ์ž„์„ ํ•˜๋‹ค


    ๋ถ€ํ„ฐCodewars
    ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    ์–ด๋–ค ์ˆซ์ž๋“ค์€ ์žฌ๋ฏธ์žˆ๋Š” ์„ฑ์งˆ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    89 --> 8ยน + 9ยฒ = 89 * 1
    695 --> 6ยฒ + 9ยณ + 5โด= 1390 = 695 * 2
    46288 --> 4ยณ + 6โด+ 2โต + 8โถ + 8โท = 2360688 = 46288 * 51
    abcd๋กœ ์“ฐ์ธ ์ •์ˆ˜ n์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.(a, b, c, d...๋Š” ์ˆซ์ž) ๋ฐ ์ •์ˆ˜ p
    ์šฐ๋ฆฌ๋Š” n์˜ ์ˆซ์ž์™€ p์˜ ์—ฐ์† ๋ฉฑ์˜ ํ•ฉ์ด k*n๊ณผ ๊ฐ™์€ ์ •์ˆ˜ k๋ฅผ ์ฐพ๊ณ  ์‹ถ๋‹ค.
    ๋‹ค์‹œ ๋งํ•˜๋ฉด
    ์˜ˆ๋ฅผ ๋“ค์–ด (a^p+b^(p+1)+c^(p+2)+d^(p+3)+d^(p+3)+...) =n*k
    ๋งŒ์•ฝ ๊ทธ๋ ‡๋‹ค๋ฉด, ์šฐ๋ฆฌ๋Š” k๋กœ ๋Œ์•„๊ฐˆ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ -1๋กœ ๋Œ์•„๊ฐ€์ง€ ์•Š๋Š”๋‹ค๋ฉด.
    ์ฃผ: n๊ณผ p๋Š” ์‹œ์ข… ์—„๊ฒฉํ•œ ์ •์ˆ˜๋กœ ์ œ์‹œ๋  ๊ฒƒ์ด๋‹ค.
    dig_pow(89, 1) should return 1 since 8ยน + 9ยฒ = 89 = 89 * 1
    dig_pow(92, 1) should return -1 since there is no k such as 9ยน + 2ยฒ equals 92 * k
    dig_pow(695, 2) should return 2 since 6ยฒ + 9ยณ + 5โด= 1390 = 695 * 2
    dig_pow(46288, 3) should return 51 since 4ยณ + 6โด+ 2โต + 8โถ + 8โท = 2360688 = 46288 * 51
    
    ๋‚ด ์†”๋ฃจ์…˜:
    function digPow(n, p){
      if(!n || !p){
        return -1;
      }
      let digitArray = n.toString().split("");
      let sun = 0;
      for(let i = 0; i<digitArray.length; i++){
        sun += Math.pow(digitArray[i], p+i);
    
      }
      if(parseInt(sun/n) === sun/n){
      return sun/n;
      }
      else{
        return -1;
      }
    }
    

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ