Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Find the Length of an Array in Python
Programming

Find the Length of an Array in Python

Last Updated on March 22, 2022 by Editorial Team

Author(s): Pratik Shukla

Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.

Photo by Diana Polekhina onΒ UnsplashΒ 

Python Programming 101 Tutorialsβ€Šβ€”β€ŠA Pure Logical Approach

In this series of articles, we will implement some basic practice problems in Python. Our ultimate goal is to improve logical thinking. So, we will not be using any Python libraries in the implementation. Please let us know if you have any suggestions or comments.

Let’s getΒ started!

Practice ProblemΒ #1:

You are given an array arr of n elements. Your goal is to find the length n of the array without using any inbuilt functions like len(). Take the following input and output as an example.
Input = [1,2,3,4,5]
Output =Β 5

Now, take some time to understand the problem and implement it inΒ Python.

A solution to Practice ProblemΒ #1:

An Explanation for the Solution of Practice ProblemΒ #1:

  • First of all, we are creating a function find_len(arr). The function takes an array arr as an argument. Our goal is to find the length of the arrayΒ arr.
  • Next, we define a variable count and set its value to 0. In the end, we will return the updated value of countas the length of the input array. We are initializing it with 0 because, at the moment, we don’t know the size of the inputΒ array.
  • Next, we are running a for loop to go through all the elements of the array. Please note that the for loop will terminate only after iterating through all the elements of the inputΒ array.
  • As we iterate through the elements of the array, we will increment the value of count by 1 to keep track of the number of elements in theΒ array.
  • Once the for loop terminates, we return the value of count. The count variable here gives us the length of the input arrayΒ arr.
  • In the next step, we are just calling the function and printing theΒ value.

Let’s have a look at how to operation is performed with some graphics.

πŸ“š Check out our editorial recommendations for the best deep learning workstations. πŸ“š

The green nodes represent the current position of the iteration. The nodes colored in red represent visitedΒ nodes.

  • Initially, no nodes are visited, and we have not entered the forΒ loop.
  • The 1st iteration of the forΒ loop.
  • The 2nd iteration of the forΒ loop.
  • The 3rd iteration of the forΒ loop.
  • The 4th iteration of the forΒ loop.
  • The 5th iteration of the forΒ loop.
  • Since there are no more elements to iterate, our for loop will terminate and return the current value ofΒ count.

So, this is how we can find the length of an array without using any inbuilt functions in Python. We hope you guys learned something new from it. If you have any suggestions or comments, please feel free to leave a comment. Thanks forΒ reading!

Resources:


Find the Length of an Array in Python was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

 

Join thousands of data leaders on the AI newsletter. It’s free, we don’t spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓