본문 바로가기

Algorithm/Code Fights (Code Signal)

Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive


Recursive solotion

재귀풀이



Q.


 You are given an array of desired filenames in the order of their creation. Since two files cannot have equal names, the one which comes later will have an addition to its name in a form of (k), where k is the smallest positive integer such that the obtained name is not used yet.

Return an array of names that will be given to the files.

 만든 순서에 따라서 만들어진 적당한 파일이름의 배열을 입력받았다. 두개의 파일은 같은 이름을 가질 수 없기 때문에, 나중에 오는 것들은 (k)가 붙는데, k 는 아직 쓰이지 않은 최소의 정수이다.

 파일들에 주어질 이름들의 배열을 만들어라


e.g.


Input ->  names = ["doc", "doc", "image", "doc(1)", "doc"]


Output -> fileNaming(names) = ["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"]




//Process

   
//1. Input fileNames vector
//2. Iterate from begin to the end
// 2.1. 


//1. 파일 이름들 배열을 입력받는다.
//2. 시작부터 끝까지 반복한다.
// 2.1. 




Code.. Lemme see code!!!!!


코드.. 코드를 보자!!!!!






std::vector<std::string> fileNaming(std::vector<std::string> names) {


    std::vector<std::string> returnVector;

    

    for (int i = 0; i < names.size(); ++i) 

    {

        int count = 0;

        makeName(&returnVector, names[i], count);

    }

    

    return returnVector;

}


void makeName(std::vector<std::string> *returnVector, string name, int count) {

    if (hasSame(returnVector, name)) 

    {

        if (count == 0) 

        {

            ++count;

            name = name + "(" + itos(count) + ")";

        } 

        else 

        {

            ++count;

            int digitCount = digitNumber(count-1);

            

            name.replace(name.length()-1-digitCount, digitCount, itos(count));

        }

        makeName(returnVector, name, count);

    } 

    else 

    {

        returnVector->push_back(name);

    }

}


int digitNumber(int number) {

    int digitCount = 0;

    while (number > 0) 

    {

        ++digitCount;

        number /= 10;

    }

    return digitCount;

}


bool hasSame(vector<string> *returnVector, string name) {

    bool answer = false;

    for (int i = 0; i < returnVector->size(); ++i) 

    {

        if (name == returnVector->at(i)) 

        {

            answer = true;

        }

    }

    return answer;

}


string itos(int integer) {

    std::stringstream ss;

    ss << integer;

    return ss.str();

}





Something else you might like..



2018/12/26 - [Programming/Design Pattern ] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍



2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #56 digitsProduct. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #55 differentSquares. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

2019/01/02 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #54 sumUpNumbers. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

2019/01/02 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #53 validTime. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

2019/01/02 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #52 longestWord. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2019/01/01 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #51 deleteDigit. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/31 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #50 chessKnight. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/28 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #49 lineEncoding. Algorithm, 알고리즘 문제풀이, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #48 isDigit. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #47 isMAC48Address. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/25 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #46 electionsWinners. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #45 buildPalindrome. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/20 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #44 findEmailDomain. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/17 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #43 isBeautifulString. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #42 bishopAndPawn. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/14 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #41 digitDegree. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #40 longestDigitsPrefix. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar

2018/12/10 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #39 knapsackLight. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar



2018/12/20 - [Programming/Java] - How to convert file to byte array (byte[]) example, 파일 객체 바이트로 변환하기 예제코드, java/cpp/c++/scalar


2018/12/18 - [Programming/Java] - How to access controller from existing Pane in JavaFX, pane에서 해당 controller로 접근하기, java/cpp/c++/fx/gui


2018/12/13 - [Computer/Linux] - How to check ongoing live process on Linux cmd, 리눅스 커맨드창에서 어떤 프로세스가 실행중인지 확인하기, java, ubuntu, cpp



2018/12/27 - [Life/Health care] - Milk-Thistle 밀크시슬 usage/side effects/dosage/fatigue/supplement,효능/부작용/성인,소아 용법/건강/피로회복/영양제

2018/12/16 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(2)

2018/12/06 - [Life/Health care] - Vitamin C 비타민 씨 usage/side effects/dosage 용법/효능/부작용

2018/12/02 - [Life/Health care] - Maca 마카 usage/side effects/dosage 효능/부작용/용법