Məzmuna keçin

Kompüter elmi

Kompüter elmləri əsaslı müzakirə kateqoriyası

24 Mövzu 30 Yazı

Alt kateqoriyalar


  • Data strukturu, verilənlərin yaddaşda səmərəli şəkildə istifadə olunması üçün təşkil edilmə üsuludur.

    7 10
    7 Mövzu
    10 Yazı
    codexC
    Doubly Linked List-in Typescript-də full implementasiyası // Node classı class Node<T> { value: T; // Dəyər next: Node<T> | null; // Növbəti qovşağa işarə prev: Node<T> | null; // Əvvəlki qovşağa işarə constructor(value: T) { this.value = value; this.next = null; this.prev = null; } } // DLL classı class DoublyLinkedList<T> { head: Node<T> | null; // Baş qovşaq tail: Node<T> | null; // Sonuncu qovşaq length: number; // Siyahının uzunluğu constructor() { this.head = null; this.tail = null; this.length = 0; } // Siyahının sonuna element əlavə etmək push(value: T): this { const newNode = new Node(value); if (!this.head) { // Siyahı boşdursa, həm head, həm tail eyni node olur this.head = newNode; } else { if (this.tail) { this.tail.next = newNode; newNode.prev = this.tail; } } this.tail = newNode; this.length++; return this; } // Siyahının sonundan element silmək pop(): Node<T> | undefined { if (!this.head) return undefined; const removed = this.tail!; if (this.length === 1) { // Siyahıda tək element varsa, hər ikisi də `null` olur this.head = null; this.tail = null; } else { this.tail = removed.prev; if (this.tail) this.tail.next = null; removed.prev = null; } this.length--; return removed; } // Siyahının əvvəlindən element silmək shift(): Node<T> | undefined { if (!this.head) return undefined; const removed = this.head; if (this.length === 1) { this.head = null; this.tail = null; } else { this.head = removed.next; if (this.head) this.head.prev = null; removed.next = null; } this.length--; return removed; } // Siyahının əvvəlinə element əlavə etmək unshift(value: T): this { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.head.prev = newNode; newNode.next = this.head; this.head = newNode; } this.length++; return this; } // Verilmiş indeksə əsasən qovşağı əldə etmək get(index: number): Node<T> | null { if (index < 0 || index >= this.length) return null; let current: Node<T>; let count: number; // İndeks siyahının ortasından əvvəldirsə, başdan axtar if (index <= this.length / 2) { current = this.head!; count = 0; while (count !== index) { current = current.next!; count++; } } else { // Ortanı keçibsə, sondan axtar current = this.tail!; count = this.length - 1; while (count !== index) { current = current.prev!; count--; } } return current; } // Verilmiş indeksdəki qovşağın dəyərini dəyişmək set(index: number, value: T): boolean { const node = this.get(index); if (node) { node.value = value; return true; } return false; } // Verilmiş indeksə yeni qovşaq əlavə etmək insert(index: number, value: T): boolean { if (index < 0 || index > this.length) return false; if (index === 0) return !!this.unshift(value); if (index === this.length) return !!this.push(value); const newNode = new Node(value); const before = this.get(index - 1); const after = before!.next; before!.next = newNode; newNode.prev = before; newNode.next = after; if (after) after.prev = newNode; this.length++; return true; } // Verilmiş indeksdəki qovşağı silmək remove(index: number): Node<T> | undefined { if (index < 0 || index >= this.length) return undefined; if (index === 0) return this.shift(); if (index === this.length - 1) return this.pop(); const removed = this.get(index)!; const before = removed.prev!; const after = removed.next!; before.next = after; after.prev = before; removed.next = null; removed.prev = null; this.length--; return removed; } // Siyahını array kimi qaytarmaq (debug üçün) toArray(): T[] { const result: T[] = []; let current = this.head; while (current) { result.push(current.value); current = current.next; } return result; } } Jenny “bajı” qəşşəng başa salır. Baxmağa dəyər https://www.youtube.com/watch?v=nquQ_fYGGA4
  • Alqoritm, adətən müəyyən bir problem sinfini həll etmək və ya hesablama aparmaq üçün istifadə olunan, riyazi cəhətdən dəqiq təlimatlardan ibarət sonlu bir ardıcıllıqdır.

    6 7
    6 Mövzu
    7 Yazı
    codexC
    Radix Sort-un Java proqramlaşdırma dilində implementasiyası import java.util.ArrayList; import java.util.List; public class RadixSort { // Ədədin müəyyən mövqedəki rəqəmini qaytarır public static int getDigit(int num, int place) { return (int)(Math.abs(num) / Math.pow(10, place)) % 10; } // Ədədin neçə rəqəmdən ibarət olduğunu tapır public static int digitCount(int num) { if (num == 0) return 1; return (int)Math.floor(Math.log10(Math.abs(num))) + 1; } // Massivdə ən çox rəqəmə sahib ədədin rəqəm sayını tapır public static int mostDigits(int[] nums) { int maxDigits = 0; for (int num : nums) { maxDigits = Math.max(maxDigits, digitCount(num)); } return maxDigits; } // Radix Sort-un əsas funksiyası public static int[] radixSort(int[] nums) { int maxDigitCount = mostDigits(nums); for (int k = 0; k < maxDigitCount; k++) { List<List<Integer>> digitBuckets = new ArrayList<>(); // 0-dan 9-a qədər 10 bucket (siyahı) yaradılır for (int i = 0; i < 10; i++) { digitBuckets.add(new ArrayList<>()); } // Ədədlər müvafiq bucket-lərə yerləşdirilir for (int num : nums) { int digit = getDigit(num, k); digitBuckets.get(digit).add(num); } // Bütün bucket-lər birləşdirilərək yeni massiv yaradılır int index = 0; for (List<Integer> bucket : digitBuckets) { for (int num : bucket) { nums[index++] = num; } } } return nums; } // Test məqsədli əsas metod public static void main(String[] args) { int[] data = {170, 45, 75, 90, 802, 24, 2, 66}; int[] sorted = radixSort(data); for (int num : sorted) { System.out.print(num + " "); } } } Nəticə: 2 24 45 66 75 90 170 802
  • Dizayn Nümunələri (Design Patterns) proqram təminatı dizaynında tez-tez baş verən problemlərin tipik həlləridir.

    6 7
    6 Mövzu
    7 Yazı
    codexC
    Proxy Pattern — obyektə birbaşa çıxışı nəzarət altında saxlamaq üçün istifadə olunan dizayn pattern-dir. Bu pattern-in əsas məqsədi, obyektə vasitəçi (proxy) vasitəsilə nəzarət etməkdir. JavaScript-də bu konsept həm real həyatda, həm də proqramlaşdırmada çox faydalıdır. Bu yazıda aşağıdakı Proxy Pattern növlərini incələyəcəyik: Property Proxy Protection Proxy Virtual Proxy 1. valueOf() və toString() ilə xüsusi tip obyektlər Aşağıdakı Percentage class-ı valueOf və toString metodları ilə say kimi istifadə edilə bilən obyekt yaradır: class Percentage { constructor(value) { this.value = value; } toString() { return `${this.value}%`; } valueOf() { return this.value / 100; } } let fivePercent = new Percentage(5); console.log(fivePercent.toString()); // "5%" console.log(`5% of 50 is ${50 * fivePercent}`); // 2.5 ️ Bu, JavaScript-in type coercion xüsusiyyətindən yararlanır: valueOf() metodu çağrıldıqda obyektin rəqəmsal dəyəri kimi işlənir. 2. Property Proxy – Dəyərlərə nəzarət və loglama Property classı ilə dəyərlərin dəyişməsinə nəzarət və loglama imkanı yaradılır: class Property { constructor(value, name = "") { this._value = value; this.name = name; } get value() { return this._value; } set value(newValue) { if (this._value === newValue) return; console.log(`Property ${this.name} changed from ${this._value} to ${newValue}`); this._value = newValue; } } class Creature { constructor() { this._agility = new Property(10, "agility"); } get agility() { return this._agility.value; } set agility(newValue) { this._agility.value = newValue; } } let creature = new Creature(); creature.agility = 15; // Property agility changed from 10 to 15 creature.agility = 15; // No change console.log(creature.agility); // 15 Fayda: Hər dəfə dəyər dəyişəndə loglama aparılır. Lazım olsa, undo sistemi və ya dəyişiklik tarixi də əlavə edilə bilər. 3. Protection Proxy – Təhlükəsizlik nəzarəti Aşağıdakı nümunədə bir maşını yalnız yaşa uyğun olaraq sürməyə icazə verilir: class Car { drive() { console.log("Car is being driven"); } } class CarProxy { constructor(driver) { this.driver = driver; this._car = new Car(); } drive() { if (this.driver.age >= 16) { this._car.drive(); } else { console.log("Driver is too young to drive"); } } } class Driver { constructor(age) { this.age = age; } } let car1 = new CarProxy(new Driver(15)); car1.drive(); // Driver is too young to drive let car2 = new CarProxy(new Driver(22)); car2.drive(); // Car is being driven Protection Proxy sistemə yalnız uyğun istifadəçilərin çıxışını təmin edir. Məsələn, admin panelinə giriş, API endpoint-ə çıxış və s. 4. Virtual Proxy – Tənbəl yükləmə (Lazy Loading) Image obyektinin yaddaşı çox istifadə edə biləcəyini fərz edək. Onu yalnız ilk dəfə lazım olduqda yaratmaq istərik: class Image { constructor(url) { this.url = url; console.log(`Image created with URL: ${this.url}`); } draw() { console.log(`Drawing image from ${this.url}`); } } class LazyImage { constructor(url) { this.url = url; } draw() { if (!this._image) { console.log(`Loading image from ${this.url}`); this._image = new Image(this.url); } this._image.draw(); } } function drawImage(image) { console.log("About to draw image..."); image.draw(); console.log("Done drawing the image."); } let image = new LazyImage("https://example.com/image.jpg"); drawImage(image); // Yalnız ilk dəfə draw çağırıldıqda Image yaradılır Fayda: Lazım olmayan resursların istifadəsini gecikdirməklə performansı optimallaşdırır. Nəticə Proxy Pattern JavaScript-də çox güclü bir konseptdir. Bu pattern aşağıdakı hallarda çox faydalıdır: Növ Təsvir Property Proxy Mülkət dəyişikliklərinə nəzarət, loglama Protection Proxy Giriş nəzarəti və hüquq yoxlaması Virtual Proxy Resursların tənbəl yüklənməsi (lazy loading) Bu nümunələr real həyatda istər UI komponentləri, istərsə də API istəkləri, giriş kontrol sistemləri üçün praktik imkanlar yaradır.
  • Ümumi problem həlli nümunələrin paylaşılması və mənimsəməsi üçün müzakirə kateqoriyası

    4 5
    4 Mövzu
    5 Yazı
    codexC
    Aşağıda LeetCode platformasında bu nümunə ilə həll edilə bilinən problemlərin siyahısı aşağıdakı kimidir. Əgər bildiyiniz problemlər varsa aşağıda əlavə edə bilərsiniz: 643. Maximum Average Subarray I 3. Longest Substring Without Repeating Characters 438. Find All Anagrams in a String 413. Arithmetic Slices 219. Contains Duplicate II 209. Minimum Size Subarray Sum 187. Repeated DNA Sequences 30. Substring with Concatenation of All Words Bütün problemlərin siyahısını isə buradan baxa bilərsiz.