"함수 is palindrome()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
13번째 줄: 13번째 줄:
[[분류: Java]]
[[분류: Java]]
{{참고|자바 isPalindrome()}}
{{참고|자바 isPalindrome()}}
<source lang='java'>
<syntaxhighlight lang='java'>
public class MyClass {
public class MyClass {
     static boolean isPalindrome(String s) {
     static boolean isPalindrome(String s) {
30번째 줄: 30번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[분류: PHP]]
[[분류: PHP]]
{{참고|PHP is_palindrome()}}
{{참고|PHP is_palindrome()}}
<source lang='php'>
<syntaxhighlight lang='php'>
function is_palindrome($str) { return $str == strrev($str); }
function is_palindrome($str) { return $str == strrev($str); }


42번째 줄: 42번째 줄:
var_dump( is_palindrome('hello') );
var_dump( is_palindrome('hello') );
# bool(false)
# bool(false)
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
function strrev8($str){
function strrev8($str){
     preg_match_all('/./us', $str, $ar);
     preg_match_all('/./us', $str, $ar);
60번째 줄: 60번째 줄:
var_dump( is_palindrome('hello') );
var_dump( is_palindrome('hello') );
var_dump( is_palindrome('coffee') );
var_dump( is_palindrome('coffee') );
</source>
</syntaxhighlight>


==Python==
==Python==
[[분류: Python]]
[[분류: Python]]
{{참고|파이썬 is_palindrome()}}
{{참고|파이썬 is_palindrome()}}
<source lang='python'>
<syntaxhighlight lang='python'>
def is_palindrome(word):
def is_palindrome(word):
     return word[::-1]==word
     return word[::-1]==word
79번째 줄: 79번째 줄:
print( is_palindrome('hello') )
print( is_palindrome('hello') )
print( is_palindrome('coffee') )
print( is_palindrome('coffee') )
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:33 판


1 개요

함수 is_palindrome()‎‎
  • madam → true
  • abradacadarba → true
  • java → false
  • hello → false

2 Java

public class MyClass {
    static boolean isPalindrome(String s) {
        return s.equals(new StringBuilder(s).reverse().toString()); 
    }
    public static void main(String args[]) {
        // true
        System.out.println( isPalindrome("kayak") );
        System.out.println( isPalindrome("madam") );
        System.out.println( isPalindrome("racecar") );
        System.out.println( isPalindrome("abradacadarba") );
        System.out.println( isPalindrome("토마토") );
        // false
        System.out.println( isPalindrome("java") );
        System.out.println( isPalindrome("hello") );
    }
}

3 PHP

function is_palindrome($str) { return $str == strrev($str); }

var_dump( is_palindrome('abradacadarba') );
# bool(true)
var_dump( is_palindrome('hello') );
# bool(false)
function strrev8($str){
    preg_match_all('/./us', $str, $ar);
    return join('', array_reverse($ar[0]));
}
function is_palindrome($str) { return $str == strrev8($str); }

# bool(true)
var_dump( is_palindrome('kayak') );
var_dump( is_palindrome('madam') );
var_dump( is_palindrome('racecar') );
var_dump( is_palindrome('abradacadarba') );
var_dump( is_palindrome('토마토') );

# bool(false)
var_dump( is_palindrome('hello') );
var_dump( is_palindrome('coffee') );

4 Python

def is_palindrome(word):
    return word[::-1]==word

# True
print( is_palindrome("kayak") )
print( is_palindrome("madam") )
print( is_palindrome("racecar") )
print( is_palindrome("abradacadarba") )
print( is_palindrome("토마토") )

# False
print( is_palindrome('hello') )
print( is_palindrome('coffee') )

5 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}