일전에 뭐 게임 회사에서 본 간단한 퀴즈 테스트 입니다.
0~9까지의 문자로 된 숫자를 입력 받았을 때, 이 입력 값이 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오.
sample inputs: 0123456789 01234 01234567890 6789012345 012322456789
sample outputs: true false false true false
python 2.7.8입니다. 입력값을 sorting해서 0123456789 와 비교하는 방법을 썼습니다.
n = [''.join(sorted(x)) for x in raw_input().split()]
for x in n:
print "true" if x=="0123456789" else "false",
2015年09月17日 10:43
두줄로 만들어 보았습니다.
a = input("0~9 사이의 숫자로 이루어진 문자열 입력 : ")
print('true' if len(a) == len(set(a)) == 10 else 'false')
2017年03月13日 14:00
파이썬 2.7.6 입니다.
print map(lambda n:len(set(n))==10 and len(n) ==10 ,"0123456789 01234 01234567890 6789012345 012322456789".split())
2015年11月25日 02:36
insertion = input()
palette = []
for i in range(0, len(insertion)):
palette.append(int(insertion[i]))
contrast = list(set(sorted(palette)))
if palette == contrast:
print("true")
else:
print("false")
파이썬의 set함수를 활용하면 중복된 원소를 모두 단일화할 수 있으므로, 그렇게 정리된 리스트가 원래의 숫자 입력을 리스트로 만든 것과 동일한가를 판가름하는 구조로 짜 보았습니다.
Java 8
public static void main(String[] args){
System.out.println(isDuplicated(12345678900l));
}
public static boolean isDuplicated(long number){
String s = String.valueOf(number);
return s.chars().distinct().count() != s.length();
}
2015年11月07日 07:37
파이썬입니다.
def only_one(string):
num_set = set(string) # 중복 제거
if len(string) == len(num_set) == 10: # 중복 제거 한 후의 갯수와 같고 그 갯수가 10이라면
return "true"
else:
return "false"
input_string = "0123456789 01234 01234567890 6789012345 012322456789"
for s in input_string.split(" "):
print(only_one(s), end=' ')
파이썬입니다.
input = "0123456789 01234 01234567890 6789012345 012322456789 1111111111"
for ca in input.split():
check = [0 for i in range(10)]
for s in ca:
check[int(s)] += 1
print sum(check) == 10 and all(check)
2015年10月03日 17:21
c# 입니다.
private void Form1_Load(object sender, EventArgs e)
{
String inputs = "0123456789 01234 01234567890 6789012345 012322456789";
String[] Split_input = inputs.Split(' ');
foreach (String s in Split_input)
{
if (Check_NUM(s))
Console.Write("true ");
else
Console.Write("false ");
}
}
private bool Check_NUM(String number)
{
List<Char> List_input = new List<Char>();
char[] data = number.ToCharArray();
foreach (Char d in data)
{
if (List_input.Contains(d))
return false;
List_input.Add(d);
}
if (List_input.Count == 10)
return true;
return false;
}
공백을 나눈 후 String을 Char 배열로 쪼개서 넣은다음 list클래스의 contain으로 값이 들어있는지 비교를 해보았습니다.
#include <iostream>
#include <string>
using namespace std;
int main( int argc , char** argv )
{
int array[ 10 ] = { 0 , };
string input = "012345632789";
for( size_t i = 0 ; i < input.size() ; ++i )
{
array[ input[ i ] - '0' ]++;
}
for( int i = 0 ; i < 10 ; ++i )
{
if( array[ i ] > 1 )
{
cout << "false" << endl;
return 0;
}
}
cout << "true" << endl;
return 0;
}
풀이 작성