본문 바로가기
연습장

Mexican Wave

by anothel 2021. 10. 25.

Introduction

The wave (known as the Mexican wave in the English-speaking world outside North America) is an example of metachronal rhythm achieved in a packed stadium when successive groups of spectators briefly stand, yell, and raise their arms. Immediately upon stretching to full height, the spectator returns to the usual seated position.

The result is a wave of standing spectators that travels through the crowd, even though individual spectators never move away from their seats. In many large arenas the crowd is seated in a contiguous circuit all the way around the sport field, and so the wave is able to travel continuously around the arena; in discontiguous seating arrangements, the wave can instead reflect back and forth through the crowd. When the gap in seating is narrow, the wave can sometimes pass through it. Usually only one wave crest will be present at any given time in an arena, although simultaneous, counter-rotating waves have been produced. (Source Wikipedia)

 

Task

In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up. 

 

Rules

 1.  The input string will always be lower case but maybe empty.

 2.  If the character in the string is whitespace then pass over it as if it was an empty seat

 

Example

wave("hello") => []string{"Hello", "hEllo", "heLlo", "helLo", "hellO"}

 

Solution

#include <cctype>

std::string convert(std::string s, int i) {
  char c = s[i];
  if (isspace(c)) return "";
  s[i] = std::toupper(s[i]);
  return s;
}

std::vector<std::string> wave(std::string y) {
  std::vector<std::string> result;
  std::string s;

  if (y.empty() == true)
    return result;
  else  // Do nothing

    for (size_t i = 0; i < y.length(); i++) {
      if (isspace(y[i])) continue;
      y[i] = std::toupper(y[i]);
      result.push_back(y);
      y[i] = std::tolower(y[i]);
    }

  return result;
}

 

v.jain.202의 코드

using namespace std;
std::vector<std::string> wave(std::string x){
  vector <string> op;
  string temp=x;
  for(int i=0;i<x.length();i++)
  {
    if(x[i]==' ')continue;
    temp[i] = toupper(temp[i]);
    op.push_back(temp);
    temp=x;
  }
  return op;
}

 

후기

이 문제는 예전 예능에서 한 글자씩 높게 말하는 파도를 코드로 나타내는 문제였다. 중간에 공백일 시에는 넣지 않고 건너뛰어야 하는데 이 부분이 살짝 고민이었다. 어떻게 하면 가장 깔끔하고 효율적으로 만들어낼 수 있을지..

코드를 열심히 풀어내고 남의 코드를 보고 다른 사람들은 어떻게 풀어냈는지 보는 것을 반복하다 보니, 내 코드가 점점 발전하는 것 같다. 사실 발전하는 것인지 아니면 남들과 점점 동화되어 가는 것인지 확실하진 않지만, 어쨌든 기본기를 조금씩 다지고 있다고 생각한다.

728x90

'연습장' 카테고리의 다른 글

Decode the Morse code  (0) 2021.10.27
정렬 > K번째수  (0) 2021.10.26
Rectangle into Squares  (0) 2021.10.25
Playing with digits  (0) 2021.10.21
Bit Counting  (0) 2021.10.19