Tag - Python3

1~8/68
  • 문자와 문자열 (27866)

    2023-06-20 21:57:38 주어진 문자열에서 n번째 알파벳을 출력하라. Bash read str read n echo "${str:$n-1:1}" Node.js const [str, n] = require("fs").readFileSync(0).toString().trim().split("\n"); console.log( str[n*1-1] ); Python3 text = input() n = int(input()) print( text[n-1] ) Ruby str = gets.chomp n = gets.chomp.to_i puts str[n-1]
  • 너의 평점은 (25206)

    2023-02-28 16:14:00 이런 게 진짜 있나..? Bash declare -A table=( [A+]=4.5 [A0]=4.0 [B+]=3.5 [B0]=3.0 [C+]=2.5 [C0]=2.0 [D+]=1.5 [D0]=1.0 [F]=0.0 ) s=0 t=0 for ((i=0; i<20; i++)); do read a b c if [ "$c" == "P" ]; then continue; fi b=${b%.*} ((s += b)) ((t += b * ${table[$c]/./})) done o=00000$((t * 10**4 / s)) o=${o: -6:1}.${o: -5} echo $o Node.js const table = { "A+": 4.5, A0: 4.0, "B+": 3.5, B0: 3.0, "C+": 2.5, C0: 2.0, "D+": 1.5, D0: 1.0, F: 0.0 } const input = require("fs").readFileSync(0).toString().trim().split("\n").map(x => x.split(" ")); s = 0; t = 0; for (let [a, b, c] of input) { if (c == "P") continue; s += b*1; t += b * table[c]; } console.log(t/s); +를 연산자가 아닌 문자로 인식하기 위해 따옴표로 감싼 것이다. Python3 table = { "A+": 4.5, "A0": 4.0, "B+": 3.5, "B0": 3.0, "C+": 2.5, "C0": 2.0, "D+": 1.5, "D0": 1.0, "F": 0.0, } s = 0 t = 0 for i in range(20): a, b, c = input().split() if c=="P": continue s += float(b) t += float(b)*table[c] print(t/s) Ruby table = { "A+" => 4.5, "A0" => 4.0, "B+" => 3.5, "B0" => 3.0, "C+" => 2.5, "C0" => 2.0, "D+" => 1.5, "D0" => 1.0, "F" => 0.0 } s = 0 t = 0 for i in 0...20 a, b, c = gets.chomp.split() next if c == "P" s += b.to_f t += b.to_f * table[c] end puts t/s
  • 팰린드롬인지 확인하기 (10988)

    2023-02-27 20:49:00 기러기 토마토 스위스 인도인 별똥별 우영우 ..역삼역 Bash read str rev="" cnt=${#str} while [ 0 -lt $cnt ]; do ((cnt--)) rev+=${str:$cnt:1} done test "$str" == "$rev" && echo 1 || echo 0 Node.js const str = require("fs").readFileSync(0).toString().trim(); const rev = str.split("").reverse().join(""); const o = str == rev ? 1 : 0; console.log(o); Python3 a = input() o = 1 if a == a[::-1] else 0 print(o) Ruby str = gets.chomp o = str == str.reverse ? 1 : 0 puts o
  • 바구니 순서 바꾸기 (10812)

    2023-02-27 20:04:00 i번 바구니부터 j번 바구니까지 공의 순서를 바꿔라. Bash read n m arr=() for ((i=0; i<n+1; i++)); do arr+=($i); done for ((idx=0; idx<m; idx++)); do read i j k bak=() for ((jdx=0; jdx<j-i+1; jdx++)); do x=$((i + (k-i+jdx)%(j-i+1))) bak+=(${arr[$x]}) done for ((jdx=0; jdx<${#bak[@]}; jdx++)); do arr[$jdx+$i]=${bak[$jdx]} done done echo ${arr[@]:1} Node.js const [[n, m], ...input] = require("fs").readFileSync(0).toString().trim().split("\n").map(x => x.split(" ").map(Number)); let arr = [...Array(n+1).keys()]; for (let [i, j, k] of input) { let bak = []; for (let jdx=0; jdx<j-i+1; jdx++) { x = i+(k-i+jdx)%(j-i+1); bak.push(arr[x]); } for (let jdx=0; jdx<bak.length; jdx++) { arr[jdx+i] = bak[jdx]; } } console.log( arr.slice(1, arr.length).join(" ") ); Python3 n, m = map(int, input().split()) arr = [str(i) for i in range(n+1)] for idx in range(m): i, j, k = map(int, input().split()) bak = [] for jdx in range(j-i+1): jdx = i+(k-i+jdx)%(j-i+1) bak.append( arr[jdx] ) for jdx in range(len(bak)): arr[jdx+i] = bak[jdx] print(' '.join(arr[1:])) Ruby n, m = gets.chomp.split().map {|i| i.to_i} arr = (0..n).to_a for idx in 0...m i, j, k = gets.chomp.split().map {|i| i.to_i} bak = [] for jdx in 0...j-i+1 jdx = i+(k-i+jdx)%(j-i+1) bak.push(arr[jdx]) end for jdx in 0...bak.size arr[jdx+i] = bak[jdx] end end puts arr[1, arr.size].join(" ")
  • 별 찍기 - 7 (2444)

    2023-02-24 10:02:00 한 변의 길이가 n인 마름모. Bash read n for ((i=0; i<n*2-1; i++)); do l=$((n * 2 - 1)) p=$((i * 2 + 1)) if [ $l -lt $p ]; then ((p += (l-p)*2)) fi o="" for ((j=0; j<p; j++)); do o+="*" done s=$((n - p/2 - 1)) printf "%${s}s${o}\n" done Node.js const n = Number(require("fs").readFileSync(0).toString().trim()); for (let i=0; i<n*2-1; i++) { let l = n*2-1; let p = i*2+1; if (l < p) p += (l-p)*2 let o = Array(p).fill("*").join(""); let s = Array(n-p/2-0.5).fill(" ").join(""); console.log(s + o); } Python3 n = int(input()) for i in range(n*2-1): l = n*2-1 p = i*2+1 if l<p: p += (l-p)*2 f = "{" + "0:^{}".format(l) + "}" o = ["*" for j in range(p)] output = f.format("".join(o)) print(output.rstrip()) 포맷팅에서 변수를 사용하여 문자열 수를 지정하기 위해 포맷팅을 사용한다. 각 라인 별 오른쪽에 공백이 있으면 틀렸다고 뜨기 때문에 rstrip()으로 오른쪽 공백 제거. Ruby n = gets.chomp.to_i for i in 0...n*2-1 l = n*2-1 p = i*2+1 p += (l-p)*2 if l<p o = "*" * p s = " " * (n-p/2-1) puts s + o end
  • 그대로 출력하기 (11718)

    2023-02-23 20:27:00 입력받은 값을 그대로 출력하라. Bash while :; do read str if [ "$str" == "" ]; then break fi echo "$str" done 따옴표로 묶지 않으면 출력 형식이 잘못되었다고 뜬다. Node.js const [...arr] = require("fs").readFileSync(0).toString().trim().split("\n"); console.log(arr.join("\n")); Python3 try: while 1: print(input()) except: pass Ruby begin while 1 puts gets.chomp end rescue nil end
  • 문자열 (9086)

    2023-02-23 20:23:00 첫 글자와 마지막 글자를 출력하라. Bash read t for ((i=0; i<t; i++)); do read str echo ${str::1}${str: -1} done Node.js const [t, ...arr] = require("fs").readFileSync(0).toString().trim().split("\n"); for (let str of arr) { console.log( str[0] + str[str.length-1] ); } Python3 t = int(input()) for i in range(t): a = input() print(a[0] + a[-1]) Ruby t = gets.chomp.to_i for i in 0...t str = gets.chomp puts str[0] + str[-1] end
  • 단어 길이 재기 (2743)

    2023-02-23 20:05:00 문자열의 길이는? Bash read str echo ${#str} Node.js const str = require("fs").readFileSync(0).toString().trim(); console.log( str.length ); Python3 print( len(input()) ) Ruby puts gets.chomp.size