{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists of Other Types\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=oIQdb93xewE&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=14) for this module.\n", "\n", "You've been practicing lists of strings, but you can make a list of anything! Here's a number example.\n", "\n", "## Fibonacci\n", "\n", "Fibonacci is a cool number sequence. It adds up the last two numbers up to make the next number. You start with 1 and 1\n", "1 + 1 = 2 (1, 1, 2)\n", "1 + 2 = 3 (1, 1, 2, 3)\n", "2 + 3 = 5 (1, 1, 2, 3, 5)\n", "3 + 5 = 8 (1, 1, 2, 3, 5, 8)\n", "and so on. There are lots of stuff in nature that follow this number sequence, and has lots of cool stuff if you want to look it up!\n", "\n", "> Start with the base numbers: Here's a list with just 1, 1 in it. Run it and see what happens." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\r\n", "1\r\n" ] } ], "source": [ "var fibonacciNumbers = new List {1, 1};\n", "\n", "foreach (var item in fibonacciNumbers)\n", " Console.WriteLine(item);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, you don't want just 1,1 in it! You want more of the sequence. In this code, you're using the last two numbers of the list, adding them together to make the next number, then adding it to the list.\n", "\n", "> Run the code to try it out." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\r\n", "1\r\n", "2\r\n" ] } ], "source": [ "var fibonacciNumbers = new List {1, 1}; // Starting the list off with the basics\n", "\n", "var previous = fibonacciNumbers[fibonacciNumbers.Count - 1]; // Take the last number in the list\n", "var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2]; // Take the second to last number in the list\n", "\n", "fibonacciNumbers.Add(previous + previous2); // Add the previous numbers together, and attach the sum to the end of the list\n", "\n", "foreach (var item in fibonacciNumbers) // Print out the list\n", " Console.WriteLine(item);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Count -1\n", "\n", "Why do you need to do `fibonacciNumbers.Count -1` to get the last number of the list? Well, `Count` tells you how many items are in a list. However, the index of an item starts at zero. So, if you only had one item in your list, the count would be one, but the index of the item would be 0. The index and count of the last item is always one off." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Challenge: Fibonacci to 20th number\n", "\n", "We've given you a base of code that deals with Fibonacci. Can you make a list that has the first 20 fibonacci numbers?\n", "\n", "> Make and print a list that has the first 20 fibonacci numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Challenge\r\n" ] } ], "source": [ "Console.WriteLine(\"Challenge\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tips and tricks\n", "\n", "- The final number should be 6765.\n", "- Could you make a `for` loop? A `foreach` loop? A `while` loop? Which kind of loop do you prefer and which would be more useful?\n", "- Are you getting close, but are you one number off? That's a really common issue! Remember that `>` and `>=` are similar, but they end up being one off from the other. Try playing around with that?\n", "- Remember that you're starting with two items in the list already.\n", "- Stuck? Watch the [C# 101 video](https://www.youtube.com/watch?v=oIQdb93xewE&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=14) for this module. Try out pausing once you get an idea and trying it out first before watching the rest." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "\n", "> [⏩ Next Module - Objects and Classes](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/13-Objects%20and%20Classes.ipynb)\n", ">\n", "> [⏪ Last Module - Search, Sort, and Index Lists](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/11-%20Search%2C%20Sort%2C%20and%20Index%20Lists.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=oIQdb93xewE&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=14)\n", ">\n", "> [Documentation: Arrays, Lists, and Collections](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/arrays-and-collections?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Download Visual Studio](https://visualstudio.microsoft.com/downloads/)\n", ">" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }