Skip to content

Instantly share code, notes, and snippets.

View artturijalli's full-sized avatar

Artturi Jalli artturijalli

View GitHub Profile
@artturijalli
artturijalli / removeTargets.py
Created May 12, 2022 15:03
Remove all occurrences of a specific value from a list using a while loop in Python
numbers = [1, 4, 4, 26, 4, 4, 8, 0, 4]
target = 4
i = 0
while i < len(numbers):
if numbers[i] == target:
numbers.pop(i)
i -= 1
i += 1
if "Alice" not in queue: print("Alice is not in the queue")
let ages = [20, 28, 30, 45]
var allAdults = true
for age in ages {
if age <= 18 {
allAdults = false
break
}
}
# How would you count the numbers in this list in as few lines of code as possible?
numbers = [1, 2, 3, 4, 5]
# 1. Charlie sums the numbers up using a for loop:
total = 0
for number in numbers:
total += number
# 2. Bob sums the numbers up using the reduce function:
from functools import reduce
class Fruit { var name = "Banana" }
let fruit1 = Fruit()
let fruit2 = fruit1
print(fruit1 === fruit2)
let a = 1
let b = 1
print(a == b)
struct Fruit {
let name: String
let color: String
}
var banana = Fruit(name: "Banana", color: "Yellow")
print(banana.name, banana.color)
struct Fruit {
let name: String
let color: String
init(name: String, color: String) {
self.name = name
self.color = color
}
}
var a = 1
var b = 2
(a, b) = (b, a)
print(a, b)
let coords = (x: 1, y: 3, z: 2)
let (x, y, z) = coords