diff --git a/Python/Password_Generator.py b/Python/Password_Generator.py deleted file mode 100644 index 92bb3fb0..00000000 --- a/Python/Password_Generator.py +++ /dev/null @@ -1,42 +0,0 @@ -import random -import string - -def generate_password(length=12, use_special=True): - """Generate a secure random password.""" - letters = string.ascii_letters - digits = string.digits - specials = string.punctuation if use_special else "" - all_chars = letters + digits + specials - - if length < 4: - print("Password length too short, minimum is 4.") - return "" - - password = [ - random.choice(string.ascii_lowercase), - random.choice(string.ascii_uppercase), - random.choice(string.digits), - random.choice(string.punctuation) if use_special else random.choice(string.ascii_letters) - ] - - password += random.choices(all_chars, k=length - 4) - random.shuffle(password) - - return "".join(password) - -def main(): - print("===== Password Generator =====") - try: - length = int(input("Enter password length (default 12): ") or 12) - except ValueError: - print("Invalid input, using default length 12.") - length = 12 - - use_special = input("Include special characters? (y/n): ").strip().lower() == 'y' - - pwd = generate_password(length, use_special) - if pwd: - print(f"\nGenerated Password: {pwd}\n") - -if __name__ == "__main__": - main() diff --git a/javascript/lru-cache.js b/javascript/lru-cache.js new file mode 100644 index 00000000..a68e2989 --- /dev/null +++ b/javascript/lru-cache.js @@ -0,0 +1,67 @@ +// lru-cache.js +// Tiny LRU cache with O(1) get/put using Map (insertion order). +// API: get/put/has/delete/clear/size. Returns -1 on miss to match your original. + +export class LRUCache { + /** + * @param {number} capacity - max number of entries (>=1) + */ + constructor(capacity) { + if (!Number.isInteger(capacity) || capacity < 1) { + throw new Error("capacity must be an integer >= 1"); + } + this.capacity = capacity; + this.cache = new Map(); // key -> value (MRU at the end) + } + + /** + * @param {any} key + * @returns {any} value or -1 if not found + */ + get(key) { + if (!this.cache.has(key)) return -1; + const value = this.cache.get(key); + // move to MRU + this.cache.delete(key); + this.cache.set(key, value); + return value; + } + + /** + * @param {any} key + * @param {any} value + */ + put(key, value) { + if (this.cache.has(key)) this.cache.delete(key); + this.cache.set(key, value); + if (this.cache.size > this.capacity) { + // evict LRU (first key) + const lruKey = this.cache.keys().next().value; + this.cache.delete(lruKey); + } + } + + /** + * @param {any} key + * @returns {boolean} + */ + has(key) { + return this.cache.has(key); + } + + /** + * @param {any} key + * @returns {boolean} + */ + delete(key) { + return this.cache.delete(key); + } + + clear() { + this.cache.clear(); + } + + get size() { + return this.cache.size; + } +}