Apply zig fmt to exercises

This commit is contained in:
Will Clardy 2021-02-15 16:55:44 -05:00
parent 97ae27435b
commit 238beb4a2d
37 changed files with 152 additions and 144 deletions

View file

@ -19,4 +19,3 @@ const std = @import("std");
fn main() void {
std.debug.print("Hello world!\n", .{});
}

View file

@ -47,5 +47,5 @@ pub fn main() void {
// is a string. The string may contain placeholders '{}', and the
// second parameter is an "anonymous list literal" (don't worry
// about this for now!) with the values to be printed.
std.debug.print("{} {} {}\n", .{n, pi, negative_eleven});
std.debug.print("{} {} {}\n", .{ n, pi, negative_eleven });
}

View file

@ -46,6 +46,7 @@ pub fn main() void {
// Use the len property to get the length of the array:
const length = some_primes.???;
std.debug.print("First: {}, Fourth: {}, Length: {}\n",
.{first, fourth, length});
std.debug.print("First: {}, Fourth: {}, Length: {}\n", .{
first, fourth, length,
});
}

View file

@ -33,8 +33,7 @@ pub fn main() void {
const major_tom = major ??? tom;
// That's all the problems. Let's see our results:
std.debug.print("d={u} {s}{s}\n",.{d, laugh, major_tom});
//
std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom });
// Keen eyes will notice that we've put 'u' and 's' inside the '{}'
// placeholders in the format string above. This tells the
// print() function to format the values as a UTF-8 character and

View file

@ -20,5 +20,5 @@ pub fn main() void {
And the Spiders from Mars
;
std.debug.print("{s}\n",.{lyrics});
std.debug.print("{s}\n", .{lyrics});
}

View file

@ -14,4 +14,3 @@ pub fn main() void {
std.debug.print("With the discount, the price is ${}.\n", .{price});
}

View file

@ -21,7 +21,7 @@ pub fn main() void {
var n: u32 = 2;
// Please use a condition that is true UNTIL "n" reaches 1024:
while ( ??? ){
while (???) {
// Print the current number
std.debug.print("{} ", .{n});

View file

@ -21,11 +21,11 @@ pub fn main() void {
// I want to print every number between 1 and 20 that is NOT
// divisible by 3 or 5.
while (n <= 20) : (n+=1) {
while (n <= 20) : (n += 1) {
// The '%' symbol is the "modulo" operator and it
// returns the remainder after division.
if(n % 3 == 0) ???;
if(n % 5 == 0) ???;
if (n % 3 == 0) ???;
if (n % 5 == 0) ???;
std.debug.print("{} ", .{n});
}

View file

@ -17,8 +17,8 @@ pub fn main() void {
// Oh dear! This while loop will go forever!?
// Please fix this so the print statement below gives the desired output.
while (true) : (n+=1) {
if(???) ???;
while (true) : (n += 1) {
if (???) ???;
}
// Result: we want n=4

View file

@ -16,13 +16,12 @@ pub fn main() void {
std.debug.print("A Dramatic Story: ", .{});
for (???) |???| {
if(scene == 'h') std.debug.print(":-) ", .{});
if(scene == 's') std.debug.print(":-( ", .{});
if(scene == 'n') std.debug.print(":-| ", .{});
if (scene == 'h') std.debug.print(":-) ", .{});
if (scene == 's') std.debug.print(":-( ", .{});
if (scene == 'n') std.debug.print(":-| ", .{});
}
std.debug.print("The End.\n", .{});
}
//
// Note that "for" loops also work on things called "slices"
// which we'll see later.

View file

@ -19,10 +19,10 @@ function main() void {
??? (i <= stop_at) : (i += 1) {
if (i % 3 == 0) std.debug.print("Fizz", .{});
if (i % 5 == 0) std.debug.print("Buzz", .{});
if ( !(i % 3 == 0) and !(i % 5 == 0) ) {
if (!(i % 3 == 0) and !(i % 5 == 0)) {
std.debug.print("{}", .{???});
}
std.debug.print(", ", .{});
}
std.debug.print("\n",.{});
std.debug.print("\n", .{});
}

View file

@ -23,7 +23,6 @@ pub fn main() void {
std.debug.print("Answer to the Ultimate Question: {}\n", .{answer});
}
//
// Please define the deepThought() function below.
//
// We're just missing a couple things. One thing we're NOT missing is the

View file

@ -7,7 +7,7 @@
// ...
// }
//
const std = @import( "std" );
const std = @import("std");
pub fn main() void {
std.debug.print("Powers of two: {} {} {} {}\n", .{
@ -18,7 +18,6 @@ pub fn main() void {
});
}
//
// Please give this function the correct input parameter(s).
// You'll need to figure out the parameter name and type that we're
// expecting. The output type has already been specified for you.

View file

@ -5,16 +5,15 @@
//
// Both of these are simply labeled "loop" below.
//
const std = @import( "std" );
const std = @import("std");
pub fn main() void {
const my_numbers = [4]u16{ 5,6,7,8 };
const my_numbers = [4]u16{ 5, 6, 7, 8 };
printPowersOfTwo(my_numbers);
std.debug.print("\n", .{});
}
//
// You won't see this every day: a function that takes an array with
// exactly four u16 numbers. This is not how you would normally pass
// an array to a function. We'll learn about slices and pointers in
@ -28,7 +27,6 @@ fn printPowersOfTwo(numbers: [4]u16) ??? {
}
}
//
// This function bears a striking resemblance to twoToThe() in the last
// exercise. But don't be fooled! This one does the math without the aid
// of the standard library!

View file

@ -16,7 +16,7 @@ const MyNumberError = error{
const std = @import("std");
pub fn main() void {
var nums = [_]u8{2,3,4,5,6};
var nums = [_]u8{ 2, 3, 4, 5, 6 };
for (nums) |n| {
std.debug.print("{}", .{n});
@ -40,7 +40,7 @@ pub fn main() void {
// Notice how this function can return any member of the MyNumberError
// error set.
fn numberFail(n: u8) MyNumberError {
if(n > 4) return MyNumberError.TooBig;
if(n < 4) return MyNumberError.TooSmall; // <---- this one is free!
if (n > 4) return MyNumberError.TooBig;
if (n < 4) return MyNumberError.TooSmall; // <---- this one is free!
return MyNumberError.TooFour;
}

View file

@ -16,7 +16,7 @@
//
const std = @import("std");
const MyNumberError = error{ TooSmall };
const MyNumberError = error{TooSmall};
pub fn main() void {
var my_number: ??? = 5;
@ -27,4 +27,3 @@ pub fn main() void {
std.debug.print("I compiled!", .{});
}

View file

@ -8,13 +8,13 @@
//
const std = @import("std");
const MyNumberError = error{ TooSmall };
const MyNumberError = error{TooSmall};
pub fn main() void {
var a: u32 = addTwenty(44) catch 22;
var b: u32 = addTwenty(4) ??? 22;
var b: u32 = addTwenty(4) ??? 22;
std.debug.print("a={}, b={}", .{a,b});
std.debug.print("a={}, b={}", .{ a, b });
}
// Please provide the return type from this function.

View file

@ -23,9 +23,9 @@ pub fn main() void {
// that makeJustRight() returns a error union (for now).
var a: u32 = makeJustRight(44) catch 0;
var b: u32 = makeJustRight(14) catch 0;
var c: u32 = makeJustRight(4) catch 0;
var c: u32 = makeJustRight(4) catch 0;
std.debug.print("a={}, b={}, c={}", .{a,b,c});
std.debug.print("a={}, b={}, c={}", .{ a, b, c });
}
// In this silly example we've split the responsibility of making
@ -37,7 +37,9 @@ pub fn main() void {
// detectProblems() Returns the number or an error.
//
fn makeJustRight(n: u32) MyNumberError!u32 {
return fixTooBig(n) catch |err| { return err; };
return fixTooBig(n) catch |err| {
return err;
};
}
fn fixTooBig(n: u32) MyNumberError!u32 {
@ -65,4 +67,3 @@ fn detectProblems(n: u32) MyNumberError!u32 {
if (n > 20) return MyNumberError.TooBig;
return n;
}

View file

@ -17,13 +17,12 @@ const MyNumberError = error{
pub fn main() void {
var a: u32 = addFive(44) catch 0;
var b: u32 = addFive(14) catch 0;
var c: u32 = addFive(4) catch 0;
var c: u32 = addFive(4) catch 0;
std.debug.print("a={}, b={}, c={}", .{a,b,c});
std.debug.print("a={}, b={}, c={}", .{ a, b, c });
}
fn addFive(n: u32) MyNumberError!u32 {
//
// This function needs to return any error which might come back from detect().
// Please use a "try" statement rather than a "catch".
//
@ -37,4 +36,3 @@ fn detect(n: u32) MyNumberError!u32 {
if (n > 20) return MyNumberError.TooBig;
return n;
}

View file

@ -9,7 +9,6 @@ pub fn main() void {
for (animals) |a| printAnimal(a);
std.debug.print("done.\n", .{});
}
@ -21,9 +20,18 @@ fn printAnimal(animal: u8) void {
std.debug.print(") ", .{}); // <---- how!?
if (animal == 'g'){ std.debug.print("Goat", .{}); return; }
if (animal == 'c'){ std.debug.print("Cat", .{}); return; }
if (animal == 'd'){ std.debug.print("Dog", .{}); return; }
if (animal == 'g') {
std.debug.print("Goat", .{});
return;
}
if (animal == 'c') {
std.debug.print("Cat", .{});
return;
}
if (animal == 'd') {
std.debug.print("Dog", .{});
return;
}
std.debug.print("Unknown", .{});
}

View file

@ -15,7 +15,6 @@
//
const std = @import("std");
//
var counter: u32 = 0;
const MyErr = error{ GetFail, IncFail };
@ -25,7 +24,7 @@ pub fn main() void {
var a: u32 = makeNumber() catch return;
var b: u32 = makeNumber() catch return;
std.debug.print("Numbers: {}, {}\n", .{a,b});
std.debug.print("Numbers: {}, {}\n", .{ a, b });
}
fn makeNumber() MyErr!u32 {
@ -35,7 +34,7 @@ fn makeNumber() MyErr!u32 {
// function exits with an error:
std.debug.print("failed!\n", .{});
var num = try getNumber(); // <-- This could fail!
var num = try getNumber(); // <-- This could fail!
num = try increaseNumber(num); // <-- This could ALSO fail!

View file

@ -29,15 +29,15 @@ pub fn main() void {
for (lang_chars) |c| {
switch (c) {
1 => std.debug.print("A", .{}),
2 => std.debug.print("B", .{}),
3 => std.debug.print("C", .{}),
4 => std.debug.print("D", .{}),
5 => std.debug.print("E", .{}),
6 => std.debug.print("F", .{}),
7 => std.debug.print("G", .{}),
8 => std.debug.print("H", .{}),
9 => std.debug.print("I", .{}),
1 => std.debug.print("A", .{}),
2 => std.debug.print("B", .{}),
3 => std.debug.print("C", .{}),
4 => std.debug.print("D", .{}),
5 => std.debug.print("E", .{}),
6 => std.debug.print("F", .{}),
7 => std.debug.print("G", .{}),
8 => std.debug.print("H", .{}),
9 => std.debug.print("I", .{}),
10 => std.debug.print("J", .{}),
// ... we don't need everything in between ...
25 => std.debug.print("Y", .{}),

View file

@ -16,15 +16,15 @@ pub fn main() void {
for (lang_chars) |c| {
var real_char: u8 = switch (c) {
1 => 'A',
2 => 'B',
3 => 'C',
4 => 'D',
5 => 'E',
6 => 'F',
7 => 'G',
8 => 'H',
9 => 'I',
1 => 'A',
2 => 'B',
3 => 'C',
4 => 'D',
5 => 'E',
6 => 'F',
7 => 'G',
8 => 'H',
9 => 'I',
10 => 'J',
// ...
25 => 'Y',

View file

@ -26,9 +26,15 @@ pub fn main() void {
for (operations) |op| {
switch (op) {
1 => { current_value += 1; },
2 => { current_value -= 1; },
3 => { current_value *= current_value; },
1 => {
current_value += 1;
},
2 => {
current_value -= 1;
},
3 => {
current_value *= current_value;
},
}
std.debug.print("{} ", .{current_value});

View file

@ -23,7 +23,7 @@ const MyNumberError = error{
const std = @import("std");
pub fn main() void {
var nums = [_]u8{2,3,4,5,6};
var nums = [_]u8{ 2, 3, 4, 5, 6 };
for (nums) |num| {
std.debug.print("{}", .{num});
@ -32,7 +32,7 @@ pub fn main() void {
if (n) |value| {
std.debug.print("=4. ", .{});
} else |err| switch (err) {
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
// Please add a match for TooSmall here and have it print: "<4. "
}
}
@ -43,7 +43,7 @@ pub fn main() void {
// This time we'll have numberMaybeFail() return an error union rather
// than a straight error.
fn numberMaybeFail(n: u8) MyNumberError!u8 {
if(n > 4) return MyNumberError.TooBig;
if(n < 4) return MyNumberError.TooSmall;
if (n > 4) return MyNumberError.TooBig;
if (n < 4) return MyNumberError.TooSmall;
return n;
}

View file

@ -7,7 +7,7 @@
//
const std = @import("std");
const NumError = error{ IllegalNumber };
const NumError = error{IllegalNumber};
pub fn main() void {
const stdout = std.io.getStdOut().writer();
@ -19,6 +19,6 @@ pub fn main() void {
// Just don't modify this function. It's "perfect" the way it is. :-)
fn getNumber() NumError!u32 {
if( false ) return NumError.IllegalNumber;
if (false) return NumError.IllegalNumber;
return 42;
}

View file

@ -20,7 +20,7 @@
const std = @import("std");
// Please complete the enum!
const Ops = enum{ ??? };
const Ops = enum { ??? };
pub fn main() void {
const operations = [_]Ops{
@ -29,16 +29,22 @@ pub fn main() void {
Ops.inc,
Ops.pow,
Ops.dec,
Ops.dec
Ops.dec,
};
var current_value: u32 = 0;
for (operations) |op| {
switch (op) {
Ops.inc => { current_value += 1; },
Ops.dec => { current_value -= 1; },
Ops.pow => { current_value *= current_value; },
Ops.inc => {
current_value += 1;
},
Ops.dec => {
current_value -= 1;
},
Ops.pow => {
current_value *= current_value;
},
// No "else" needed! Why is that?
}

View file

@ -26,10 +26,10 @@ const std = @import("std");
// #RRGGBB
//
// Please define and use a pure blue value Color:
const Color = enum(u32){
red = 0xff0000,
const Color = enum(u32) {
red = 0xff0000,
green = 0x00ff00,
blue = ???,
blue = ???,
};
pub fn main() void {
@ -53,9 +53,9 @@ pub fn main() void {
\\ <span style="color: #{x:0>6}">Green</span>
\\ <span style="color: #{}">Blue</span>
\\</p>
, .{
@enumToInt(Color.red),
@enumToInt(Color.green),
@enumToInt(???), // Oops! We're missing something!
});
, .{
@enumToInt(Color.red),
@enumToInt(Color.green),
@enumToInt(???), // Oops! We're missing something!
});
}

View file

@ -23,7 +23,7 @@
const std = @import("std");
// We'll use an enum to specify the character class.
const Class = enum{
const Class = enum {
wizard,
thief,
bard,
@ -32,7 +32,7 @@ const Class = enum{
// Please add a new property to this struct called "health" and make
// it a u8 integer type.
const Character = struct{
const Character = struct {
class: Class,
gold: u32,
experience: u32,
@ -41,8 +41,8 @@ const Character = struct{
pub fn main() void {
// Please initialize Glorp with 100 health.
var glorp_the_wise = Character{
.class = Class.wizard,
.gold = 20,
.class = Class.wizard,
.gold = 20,
.experience = 10,
};
@ -54,6 +54,6 @@ pub fn main() void {
std.debug.print("Your wizard has {} health and {} gold.", .{
glorp_the_wise.health,
glorp_the_wise.gold
glorp_the_wise.gold,
});
}

View file

@ -8,14 +8,14 @@
//
const std = @import("std");
const Class = enum{
const Class = enum {
wizard,
thief,
bard,
warrior,
};
const Character = struct{
const Character = struct {
class: Class,
gold: u32,
health: u8,
@ -27,9 +27,9 @@ pub fn main() void {
// Glorp the Wise
chars[0] = Character{
.class = Class.wizard,
.gold = 20,
.health = 100,
.class = Class.wizard,
.gold = 20,
.health = 100,
.experience = 10,
};
@ -45,7 +45,8 @@ pub fn main() void {
// Printing all RPG characters in a loop:
for (chars) |c, num| {
std.debug.print("Character {} - G:{} H:{} XP:{}\n",
.{num+1, c.gold, c.health, c.experience});
std.debug.print("Character {} - G:{} H:{} XP:{}\n", .{
num + 1, c.gold, c.health, c.experience,
});
}
}

View file

@ -32,5 +32,5 @@ pub fn main() void {
// (See the "cheatsheet" above for ideas.)
num2 = ???;
std.debug.print("num1: {}, num2: {}\n", .{num1, num2});
std.debug.print("num1: {}, num2: {}\n", .{ num1, num2 });
}

View file

@ -23,5 +23,5 @@ pub fn main() void {
const a: u8 = 12;
const b: *u8 = &a; // fix this!
std.debug.print("a: {}, b: {}\n", .{a, b.*});
std.debug.print("a: {}, b: {}\n", .{ a, b.* });
}

View file

@ -37,5 +37,5 @@ pub fn main() void {
p.* += 1;
p = &bar;
p.* += 1;
std.debug.print("foo={}, bar={}\n", .{foo, bar});
std.debug.print("foo={}, bar={}\n", .{ foo, bar });
}

View file

@ -12,7 +12,6 @@ pub fn main() void {
makeFive(&num);
std.debug.print("num: {}, ", .{num});
// Now something interesting. Let's pass a reference to a
// specific array value:
makeFive(&more_nums[2]);

View file

@ -34,33 +34,32 @@
//
const std = @import("std");
const Class = enum{
const Class = enum {
wizard,
thief,
bard,
warrior,
};
const Character = struct{
const Character = struct {
class: Class,
gold: u32,
health: u8 = 100, // <--- You can also fields a default value!
health: u8 = 100, // <--- You can also provide fields a default value!
experience: u32,
};
pub fn main() void {
var glorp = Character{
.class = Class.wizard,
.gold = 10,
.class = Class.wizard,
.gold = 10,
.experience = 20,
};
// FIX ME!
// Please pass our Character "glorp" to printCharacter():
printCharacter( ??? );
printCharacter(???);
}
// Note how this function's "c" parameter is a pointer to a Character struct.
fn printCharacter(c: *Character) void {
@ -68,9 +67,9 @@ fn printCharacter(c: *Character) void {
// don't have to write the full enum name. Zig understands that ".wizard"
// means "Class.wizard" when we switch on a Class enum value:
const class_name = switch (c.class) {
.wizard => "Wizard",
.thief => "Thief",
.bard => "Bard",
.wizard => "Wizard",
.thief => "Thief",
.bard => "Bard",
.warrior => "Warrior",
};

View file

@ -10,7 +10,7 @@
//
const std = @import("std"); // single quotes
const Elephant = struct{
const Elephant = struct {
letter: u8,
tail: *Elephant = undefined,
visited: bool = false,

View file

@ -31,7 +31,7 @@ pub fn main() void {
// integer value from deepThought() OR the number 42:
var answer: u8 = result;
std.debug.print("The Ultimate Answer: {}.\n",.{answer});
std.debug.print("The Ultimate Answer: {}.\n", .{answer});
}
fn deepThought() ?u8 {
@ -39,7 +39,6 @@ fn deepThought() ?u8 {
// But we'll leave this as-is. Sorry Deep Thought.
return null;
}
//
// Blast from the past:
//
// Optionals are a lot like error union types which can either