Tarkibga o'tish

4-bo'lim: Satrlar

↑ Mundarijaga qaytish

41. Satrni teskari ag'darish

JS

const reverse = s => s.split("").reverse().join("");
PHP
function reverse($s) {
    return strrev($s);
}
Python
def reverse(s):
    return s[::-1]

42. Palindrom satr

JS

const isPalindrome = s => s === s.split("").reverse().join("");
PHP
function isPalindrome($s) {
    return $s === strrev($s);
}
Python
def is_palindrome(s):
    return s == s[::-1]

43. Unli harflar soni (vowels)

JS

const vowels = s => (s.match(/[aeiou]/gi) || []).length;
PHP
function vowels($s) {
    return preg_match_all('/[aeiou]/i', $s);
}
Python
def vowels(s):
    return sum(c.lower() in "aeiou" for c in s)

44. So'zlar soni

JS

const wordCount = s => s.trim().split(/\s+/).filter(Boolean).length;
PHP
function wordCount($s) {
    return count(preg_split('/\s+/', trim($s), -1, PREG_SPLIT_NO_EMPTY));
}
Python
def word_count(s):
    return len(s.split())

45. Katta-kichik harf almashtirish (swapcase)

JS

const swapCase = s => s.replace(/[a-z]/gi, c =>
  c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase());
PHP
function swapCase($s) {
    $out = "";
    foreach (str_split($s) as $c) {
        $out .= ctype_upper($c) ? strtolower($c) : strtoupper($c);
    }
    return $out;
}
Python
def swap_case(s):
    return s.swapcase()

46. Belgi chastotasi (character frequency)

JS

const charFreq = s => {
  const f = {};
  for (const c of s) f[c] = (f[c] || 0) + 1;
  return f;
};
PHP
function charFreq($s) {
    return array_count_values(str_split($s));
}
Python
from collections import Counter

def char_freq(s):
    return dict(Counter(s))

47. Anagramma tekshirish

JS

const isAnagram = (a, b) => {
  const norm = s => s.replace(/\s/g, "").toLowerCase().split("").sort().join("");
  return norm(a) === norm(b);
};
PHP
function isAnagram($a, $b) {
    $norm = function ($s) {
        $arr = str_split(strtolower(str_replace(" ", "", $s)));
        sort($arr);
        return implode("", $arr);
    };
    return $norm($a) === $norm($b);
}
Python
def is_anagram(a, b):
    norm = lambda s: sorted(s.replace(" ", "").lower())
    return norm(a) == norm(b)

48. Eng uzun so'z

JS

const longestWord = s => s.trim().split(/\s+/).reduce((a, b) => b.length > a.length ? b : a, "");
PHP
function longestWord($s) {
    $words = preg_split('/\s+/', trim($s));
    usort($words, fn($a, $b) => strlen($b) - strlen($a));
    return $words[0];
}
Python
def longest_word(s):
    return max(s.split(), key=len)

49. Barcha bo'sh joylarni olib tashlash

JS

const stripSpaces = s => s.replace(/\s+/g, "");
PHP
function stripSpaces($s) {
    return preg_replace('/\s+/', '', $s);
}
Python
def strip_spaces(s):
    return "".join(s.split())

50. Birinchi takrorlanmas belgi

JS

const firstUnique = s => {
  for (const c of s) if (s.indexOf(c) === s.lastIndexOf(c)) return c;
  return null;
};
PHP
function firstUnique($s) {
    $counts = array_count_values(str_split($s));
    foreach (str_split($s) as $c) if ($counts[$c] === 1) return $c;
    return null;
}
Python
from collections import Counter

def first_unique(s):
    counts = Counter(s)
    for c in s:
        if counts[c] == 1:
            return c
    return None

51. Qism-satr indeksini topish

Topilmasa: JS/Python βˆ’1, PHP false.

JS

const indexOf = (s, sub) => s.indexOf(sub);
PHP
function indexOf($s, $sub) {
    return strpos($s, $sub);
}
Python
def index_of(s, sub):
    return s.find(sub)

52. Akronim (bosh harflar)

"laravel domain driven" β†’ "LDD".

JS

const acronym = s => s.trim().split(/\s+/).map(w => w[0].toUpperCase()).join("");
PHP
function acronym($s) {
    $words = preg_split('/\s+/', trim($s));
    return implode("", array_map(fn($w) => strtoupper($w[0]), $words));
}
Python
def acronym(s):
    return "".join(w[0].upper() for w in s.split())

53. Faqat harflarni qoldirish

JS

const lettersOnly = s => s.replace(/[^a-z]/gi, "");
PHP
function lettersOnly($s) {
    return preg_replace('/[^a-z]/i', '', $s);
}
Python
def letters_only(s):
    return "".join(c for c in s if c.isalpha())

54. Har bir so'zni teskari ag'darish

"abc def" β†’ "cba fed".

JS

const reverseWords = s => s.split(" ").map(w => w.split("").reverse().join("")).join(" ");
PHP
function reverseWords($s) {
    return implode(" ", array_map('strrev', explode(" ", $s)));
}
Python
def reverse_words(s):
    return " ".join(w[::-1] for w in s.split(" "))

55. camelCase β†’ snake_case

"helloWorld" β†’ "hello_world".

JS

const toSnake = s => s.replace(/[A-Z]/g, c => "_" + c.toLowerCase());
PHP
function toSnake($s) {
    return strtolower(preg_replace('/([A-Z])/', '_$1', $s));
}
Python
import re

def to_snake(s):
    return re.sub(r"[A-Z]", lambda m: "_" + m.group().lower(), s)