Skip to content

Instantly share code, notes, and snippets.

@isaacssemugenyi
Created December 16, 2025 21:19
Show Gist options
  • Select an option

  • Save isaacssemugenyi/6595996e266262d5ac6a4b0966587d4d to your computer and use it in GitHub Desktop.

Select an option

Save isaacssemugenyi/6595996e266262d5ac6a4b0966587d4d to your computer and use it in GitHub Desktop.
"""
**************************************
ADDRESS BOOK (LEGACY / MESSY VERSION)
Works, but badly designed
**************************************
"""
contacts = []
last_id = 0
def add_contact(name, phone, email, address):
global last_id
if name is None or name == "":
print("Name is required")
return False
if phone is None or phone == "":
print("Phone is required")
return False
for c in contacts:
if c["phone"] == phone:
print("Phone already exists")
return False
last_id = last_id + 1
contact = {
"id": last_id,
"name": name,
"phone": phone,
"email": email if email else "",
"address": address if address else "",
"created_at": __import__("datetime").datetime.now().isoformat()
}
contacts.append(contact)
print("Contact added:", contact["name"])
return True
def get_all_contacts():
if len(contacts) == 0:
print("No contacts found")
return []
result = []
for c in contacts:
result.append(c)
return result
def find_contact_by_phone(phone):
if not phone:
print("Phone required")
return None
for c in contacts:
if c["phone"] == phone:
return c
return None
def update_contact(phone, new_name, new_email, new_address):
found = False
for c in contacts:
if c["phone"] == phone:
if new_name:
c["name"] = new_name
if new_email:
c["email"] = new_email
if new_address:
c["address"] = new_address
c["updated_at"] = __import__("datetime").datetime.now().isoformat()
print("Contact updated")
found = True
if not found:
print("Contact not found")
return False
return True
def delete_contact(phone):
index = -1
for i in range(len(contacts)):
if contacts[i]["phone"] == phone:
index = i
if index == -1:
print("Contact not found")
return False
contacts.pop(index)
print("Contact deleted")
return True
def search_contacts(keyword):
results = []
if not keyword:
return results
for c in contacts:
if (
keyword in c["name"]
or keyword in c["phone"]
or keyword in c["email"]
):
results.append(c)
return results
# -------------------
# Demo usage
# -------------------
add_contact("John Doe", "0700000001", "john@email.com", "Kampala")
add_contact("Jane Smith", "0700000002", "", "")
update_contact("0700000001", "John D.", None, "Ntinda")
print(get_all_contacts())
/**************************************
* ADDRESS BOOK (LEGACY / MESSY VERSION)
**************************************/
let contacts = [];
let lastId = 0;
function addContact(name, phone, email, address) {
if (name == null || name === "") {
console.log("Name is required");
return false;
}
if (phone == null || phone === "") {
console.log("Phone is required");
return false;
}
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].phone === phone) {
console.log("Phone already exists");
return false;
}
}
lastId = lastId + 1;
let contact = {
id: lastId,
name: name,
phone: phone,
email: email ? email : "",
address: address ? address : "",
createdAt: new Date().toISOString()
};
contacts.push(contact);
console.log("Contact added:", contact.name);
return true;
}
function getAllContacts() {
if (contacts.length === 0) {
console.log("No contacts found");
return [];
}
let result = [];
for (let i = 0; i < contacts.length; i++) {
result.push(contacts[i]);
}
return result;
}
function findContactByPhone(phone) {
if (!phone) {
console.log("Phone required");
return null;
}
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].phone === phone) {
return contacts[i];
}
}
return null;
}
function updateContact(phone, newName, newEmail, newAddress) {
let found = false;
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].phone === phone) {
if (newName) {
contacts[i].name = newName;
}
if (newEmail) {
contacts[i].email = newEmail;
}
if (newAddress) {
contacts[i].address = newAddress;
}
contacts[i].updatedAt = new Date().toISOString();
console.log("Contact updated");
found = true;
}
}
if (!found) {
console.log("Contact not found");
return false;
}
return true;
}
function deleteContact(phone) {
let index = -1;
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].phone === phone) {
index = i;
}
}
if (index === -1) {
console.log("Contact not found");
return false;
}
contacts.splice(index, 1);
console.log("Contact deleted");
return true;
}
function searchContacts(keyword) {
let results = [];
if (!keyword) {
return results;
}
for (let i = 0; i < contacts.length; i++) {
if (
contacts[i].name.includes(keyword) ||
contacts[i].phone.includes(keyword) ||
contacts[i].email.includes(keyword)
) {
results.push(contacts[i]);
}
}
return results;
}
/*************
* Demo usage
*************/
addContact('John Doe', '0700000001', 'john@email.com', 'Kampala');
addContact("Jane Smith", "0700000002", "", "");
updateContact("0700000001", "John D.", null, "Ntinda");
console.log(getAllContacts());
console.log(findContactByPhone("0700000002"));
console.log(searchContacts("John"));
module.exports = {
addContact,
getAllContacts,
findContactByPhone,
updateContact,
deleteContact,
searchContacts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment