ASCII 문자 문자열이 있으면 가운데에있는 문자를 출력하십시오. 중간 문자가없는 경우 (문자열 길이가 짝수 인 경우) 서 수가 두 중심 문자의 바닥 평균 인 ASCII 문자를 출력하십시오. 문자열이 비어 있으면 빈 문자열이 출력되어야합니다.
테스트 사례 :
12345 => 3
Hello => l
Hiya => q
(empty input) => (empty output)
문자가 가장 짧은 프로그램이 승리합니다. (바이트가 아님)
리더 보드
이 게시물의 하단에있는 스택 스 니펫은 답변 a) 언어별로 가장 짧은 솔루션 목록으로, b) 전체 리더 보드로 답변에서 리더 보드를 생성합니다.
답변이 표시되도록하려면 다음 마크 다운 템플릿을 사용하여 헤드 라인으로 답변을 시작하십시오.
## Language Name, N characters
N
제출물의 크기는 어디에 있습니까 ? 당신이 당신의 점수를 향상시킬 경우에, 당신은 할 수 있습니다 를 통해 눈에 띄는에 의해, 헤드 라인에 오래된 점수를 유지한다. 예를 들어 :
## Ruby, <s>104</s> <s>101</s> 96 characters
헤더에 여러 숫자를 포함하려는 경우 (예 : 점수가 두 파일의 합계이거나 인터프리터 플래그 페널티를 별도로 나열하려는 경우) 실제 점수가 헤더 의 마지막 숫자 인지 확인하십시오 .
## Perl, 43 + 2 (-p flag) = 45 characters
언어 이름을 링크로 만들면 스 니펫에 표시됩니다.
## [><>](http://esolangs.org/wiki/Fish), 121 characters
<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table><script>var QUESTION_ID = 64599; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 47556; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>
답변
Pyth, 15 바이트
Cs.O@R/lz2_BCMz
“Hiya”를 입력으로 시작 :
z "Hiya" Input
CM [72, 105, 121, 97] Character values
_B [[72, 105, 121, 97], [97, 121, 105, 72]] Pair with reversal
/lz2 2 Halfway index
@R [121, 105] That element in each
.O 113.0 Average
s 113 Floor
C "q" Cast to character
q Print implicitly
이것은 빈 입력시 오류와 함께 충돌하고 STDOUT에 아무것도 인쇄하지 않습니다. 이는 코드 골프 기본값으로 빈 문자열을 출력하는 유효한 방법입니다.
답변
Brainf ***, 61 바이트
중국어 , 16 자
이를 위해서는 입력 범위가 1-127의 ASCII 범위에 있어야하며 널 종료됩니다. 하나 또는 두 개의 문자가 남을 때까지 문자열의 시작과 끝에서 문자 쌍을 삭제합니다. 두 개가 있으면 함께 더한 다음 2로 나눕니다. 나머지 문자가 인쇄됩니다.
,[>,]<<<[[<]>[-]>[>]<[-]<<<]>[[->+<]>[-[-<+<]>>]<[>]<[>]<<]>.
이 통역사 에서 사용해보십시오 .
해부:
,[>,] Get the null terminated input
<<< Move to the second last character
[ While there are at least 3 characters
[<]> Move to the first character
[-] Delete it
>[>]< Move to the last character
[-] Delete it
<<< Move the the third last character
]
> Move to the second last character
[ If there are two characters remaining
[->+<] Add the two characters together
>[-[-<+<]>>] Divide the character by 2 rounding down
<[>]<[>]<< Move to before the character to exit the if loop
]
>. Print the remaining character
* 각 명령어를 3 비트로 압축하고 UTF-32로 인코딩 할 수 있으면 전체 프로그램을 기술적으로 6 자로 표현할 수 있습니다.
편집 : Jan Dvorak 에게 중국어 를 소개해 주셔서 감사합니다. 이 문자를 Dennis의 CJam 답변 과 동등한 16 자로 압축합니다 .
蜐蕈帑聿纂胯箩悚衅鹊颂鹛拮拮氰人
답변
CJam, 16 바이트
q:i_W%.+_,2/=2/c
작동 원리
q e# Read all input.
:i e# Cast each character to integer.
_W% e# Push a reversed copy.
.+ e# Perform vectorized addition.
_,2/ e# Get the length, divided by 2.
= e# Select the corresponding sum.
2/c e# Divide by 2 and cast to character.
답변
티 스크립트 , 23 바이트 25 30 31 33
²(x[t=xn/2]c+xv[t]c)/2)
@isaacg의 문자열을 뒤집는 아이디어를 사용합니다.
Ungolfed && 설명
TeaScript는 여전히 JavaScript이므로 JavaScript와 매우 유사하게 작동합니다.
C((x[t=xn/2]c+xv[t]c)/2)
C(( // Char code to char
x[t=xn/2]c // Floored center
+ // Add to
xv[t]c // Ceil'd center
) / 2) // Divide by 2
답변
Matlab, 39 37 바이트
floor((end+[1,2])/2)
길이가 짝수이면 문자열의 가운데 두 인덱스를 반환하고 길이가 홀수이면 가운데 인덱스를 두 번 반환합니다.
mean
그 값의 평균을 반환하고 char
자동으로 그 값을 계산 합니다.
@(s)char(mean(s(floor(end/2+[.5,1]))))
답변
8086 머신 코드 + DOS, 31 바이트
16 진 덤프 :
BA 1E 01 B4 0A CD 21 8B F2 46 8A 1C D0 EB 8A 50
01 72 06 74 08 02 10 D0 EA B4 02 CD 21 C3 FF
어셈블리 소스 코드 (Tasm으로 어셈블리 가능) :
.MODEL TINY
.CODE
org 100h
MAIN PROC
mov dx, offset buf
mov ah, 10 ; DOS function "read string"
int 21h
mov si, dx
inc si ; si now points to the length of the string
mov bl, [si] ;
shr bl, 1 ; divide the length by 2
mov dl, [si+bx+1] ; load the middle char
jc calc_done ; if length was odd - done
jz output_done ; if length was zero - exit
add dl, [si+bx] ; add the other middle char
shr dl, 1 ; divide by 2
calc_done:
mov ah, 2 ; DOS function "output char"
int 21h
output_done:
ret
buf db 255 ; maximum bytes to input
MAIN ENDP
END MAIN
FLAGS 레지스터의 섬세한 사용법이 여기에 있습니다. 문자열 길이를 1 비트만큼 오른쪽으로 이동 한 후 (2로 나누는 것과 동일) 두 플래그는 추가 정보를 저장합니다.
- 캐리 플래그 : 시프트 된 비트를 포함합니다. 비트가 1이면 길이가 홀수입니다.
- 제로 플래그 : 결과가 0인지 여부를 나타냅니다. carry 플래그가 0이고 zero 플래그가 1 인 경우, 길이는 0이므로 아무 것도 인쇄하지 않아야합니다.
일반적으로 플래그는 즉시 확인해야하지만 여기서는 mov
명령이 플래그를 변경하지 않는다는 사실을 사용합니다 . 중간 문자를로드 한 후 검사 할 수 있습니다.