- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AINeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
A multi-armed bandit is a row of slot machines (arms) with unknown payouts. Epsilon-greedy balances exploring and exploiting: with probability epsilon it pulls a random arm, otherwise it pulls the arm with the highest estimated value.
To make the run testable, the random numbers are given. Start with an estimate Q = 0 and a count N = 0 for every arm. At each step t:
- If
random_values[t]< epsilon, explore: arm =random_arms[t]. Otherwise exploit: arm = the arm with the highest Q (the lowest index if several tie). - Receive reward =
rewards[t][arm]. - N[arm] ← N[arm] + 1 and Q[arm] ← Q[arm] + (reward - Q[arm]) / N[arm], the running average.
Implement epsilon_greedy(rewards, epsilon, random_values, random_arms).
Input
rewards: One row per step with the reward each arm would pay at that step.
epsilon: The exploration probability.
random_values: One random number in [0, 1) per step.
random_arms: The arm to pull at each step if exploring.
Output
Return the final Q estimates, one per arm.
Constraints
1 ≤ steps ≤ 10,000, 1 ≤ arms ≤ 100
0 ≤ epsilon ≤ 1, -100 ≤ each reward ≤ 100
Examples
Example 1
Input:
rewards = [
[1.0, 3.0],
[1.0, 3.0],
[1.0, 3.0],
[1.0, 3.0]
]
epsilon = 0.1
random_values = [0.5, 0.05, 0.7, 0.9]
random_arms = [0, 1, 0, 0]
Expected output:
[1.0, 3.0]
Explanation
Step 0 exploits; both Q are 0, so arm 0 is pulled and Q = [1, 0]. Step 1 explores (0.05 < 0.1) and tries arm 1: Q = [1, 3]. Steps 2 and 3 exploit the better arm 1. Result: [1, 3].
Example 2
Input:
rewards = [
[2.0, 5.0],
[4.0, 5.0],
[0.0, 5.0]
]
epsilon = 0.0
random_values = [0.5, 0.5, 0.5]
random_arms = [1, 1, 1]
Expected output:
[2.0, 0.0]
Explanation
With epsilon = 0 the agent never explores. It keeps pulling arm 0, whose average goes 2 → 3 → 2, and never finds out arm 1 pays 5. Result: [2, 0].
References
-
Example 1 Not runInput:
rewards = [ [1.0, 3.0], [1.0, 3.0], [1.0, 3.0], [1.0, 3.0] ] epsilon = 0.1 random_values = [0.5, 0.05, 0.7, 0.9] random_arms = [0, 1, 0, 0]Expected output:[1.0, 3.0]Your output -
Example 2 Not runInput:
rewards = [ [2.0, 5.0], [4.0, 5.0], [0.0, 5.0] ] epsilon = 0.0 random_values = [0.5, 0.5, 0.5] random_arms = [1, 1, 1]Expected output:[2.0, 0.0]Your output
4 hidden tests also run when you submit. If your code fails one, you'll see that test. Solve the challenge to see them all.
Run Swift Online
Write, run, and debug Swift code online using a command-line Swift environment. Manage multiple files, save your workspace, and share code without installing Xcode.
🚀 19,043 total executions (611 this month)
Practice Swift with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
🍎 About This Swift Online Executor
The CodeUtility Swift Executor lets you write and run Swift code directly in your browser - no Xcode, SDK, or macOS setup required. It runs real Swift compilers in isolated sandboxes to deliver accurate, native Swift behavior.
This tool supports multiple Swift versions - 5.7, 5.9, and the latest release - giving you the flexibility to explore new language features and maintain compatibility with older Swift codebases.
It’s perfect for developers learning Swift syntax, testing algorithms, or experimenting with code for iOS, macOS, and server-side Swift without needing to install local tools or simulators.
Each snippet runs securely in a containerized Swift environment, ensuring real compiler feedback and safe code execution.
More than a code runner
Use a multi-file editor, an isolated execution terminal, runtime-specific libraries, persistent workspaces, sharing, and real-time collaboration from the same executor.
How to use this executor?
Choose a runtime, open or create a file, write your code, and select Run. Output appears in the terminal, while signed-in users can keep files in a workspace, install libraries, and collaborate through shares.
- Select a version and edit the current file or open another workspace file.
- Run the code, provide input in the terminal when requested, and review saved run history.
- Use libraries, workspace tools, sharing, and chat when your task needs more than one snippet.
💡 Swift Basics Guide for Beginners
1. Declaring Variables and Constants
Use var for variables and let for constants in Swift.
var age = 30
let pi = 3.14159
var name = "Alice"
let isActive = true
// Constants cannot be reassigned
// pi = 3.14 // ❌ Error
2. Conditionals (if / switch)
Use if, else if, else for logic, and switch for multiple branches.
let number = 2
if number == 1 {
print("One")
} else if number == 2 {
print("Two")
} else {
print("Other")
}
switch number {
case 1:
print("One")
case 2:
print("Two")
default:
print("Other")
}
3. Loops
Swift supports for-in, while, and repeat-while loops.
for i in 0..<3 {
print(i)
}
var n = 3
while n > 0 {
print(n)
n -= 1
}
4. Arrays
Arrays store ordered collections of values.
var numbers = [10, 20, 30]
print(numbers[1])
5. Array Manipulation
Use array methods like append and removeLast.
var nums = [1, 2, 3]
nums.append(4)
nums.removeLast()
for n in nums {
print(n)
}
6. Console Input/Output
Use print() for output. Input requires additional work in Swift Playgrounds or Xcode.
let name = "Alice"
print("Hello, \(name)")
7. Functions
Define reusable functions with parameters and return values.
func add(a: Int, b: Int) -> Int {
return a + b
}
print(add(a: 3, b: 4))
8. Dictionaries
Dictionaries store key-value pairs.
var ages = ["Alice": 30]
print(ages["Alice"] ?? 0)
9. Error Handling
Swift uses do-catch for error handling.
enum MyError: Error {
case runtimeError(String)
}
do {
throw MyError.runtimeError("Something went wrong")
} catch let error {
print(error)
}
10. File I/O
Swift uses FileManager or String methods for file I/O.
let text = "Hello File"
try text.write(toFile: "file.txt", atomically: true, encoding: .utf8)
let content = try String(contentsOfFile: "file.txt")
print(content)
11. String Manipulation
Use methods like count, contains, uppercased(), etc.
let text = "Hello World"
print(text.count)
print(text.contains("World"))
print(text.uppercased())
12. Classes & Objects
Swift supports OOP using class and init.
class Person {
var name: String
init(name: String) {
self.name = name
}
func greet() {
print("Hi, I'm \(name)")
}
}
let p = Person(name: "Alice")
p.greet()
13. Optionals
Swift uses ? and ! to handle optional values.
var name: String? = "Alice"
print(name ?? "Unknown")
if let unwrappedName = name {
print(unwrappedName)
}