Skip to main content

CT Week 3

Summary

📚

Summary

Iterators and Variables

Iterators can be used to repeat activities systematically, ensuring each item (like a card) is visited exactly once. Variables are used to maintain extra information during iteration. Unlike constants, variables can change their values during the iteration.

Generic Flowchart for Iterator with Variables

We modify the iterator flowchart to include variables to store intermediate information. The basic structure remains the same, but we need to initialize the variables before the iteration loop starts and update them using the values from the current item at each step inside the loop.

Counting Example

To count the number of cards in a pile, we use a variable named count, initialized to 0. As we go through each card, we increment count by 1. The flowchart shows initialization, iteration, and update steps.

Sum Example

To find the sum of values from a specific field on the cards, we use a variable named sum, initialized to 0. We add the value of the relevant field from each card to sum. The flowchart is similar to the counting flowchart but updates sum with the field value from each card.

Average

Average

To find the average value of a field from all the cards in a dataset, you need to:

  1. Calculate the total value of the field (sum).
  2. Count the number of cards (count).
  3. Divide the total value by the number of cards to get the average.

Initially, this process requires two passes through the cards: one to find the sum and another to find the count. However, you can optimize this by maintaining both the sum and count variables simultaneously during a single pass through the cards.

Generic Flowchart for Average

The computation of the average can be done after iterating through all the cards, rather than during the iteration. This generic flowchart can be adapted for different fields, such as physics marks or letter counts, by replacing the specific field in the flowchart.

Collecting Values in a List

Variables can store different types of data, not just numbers. For example, you can collect field values into a list. A list can contain multiple occurrences of the same element. The flowchart for collecting values into a list initializes an empty list and appends each value to the list during iteration.

Accumulator Summary

Accumulator

The concept of an accumulator involves a variable that accumulates values during iteration. This pattern is seen in various scenarios, such as summing total marks or collecting a list of physics marks.

In both cases, the accumulator variable is initialized to a value representing "empty" (e.g., 0 for sum, [] for a list). The variable used in this pattern is called an accumulator variable.

Examples of accumulation include:

  • Addition: Summing values.
  • Appending: Collecting items into a list.
  • Multiplication: Finding the product of values.

In general, an accumulator pattern can involve processing each element through a map operation followed by an accumulation operation (also known as the reduce operation).

For example, to find the total number of items purchased by all customers in a shopping bill dataset, the map operation would count the number of item rows in each bill, and the reduce operation would sum these counts to get the total number of packed items purchased.

Procedure Summary

A procedure, called as a separate statement, may not return a value but helps modularize pseudocode by handling repeated computations in different contexts. Parameters set the context, variables store returned values, and improvements benefit all uses automatically.

Embed PDFs

GA

PA:

Comments

Popular post

IITM Notes

Course Overview “These handwritten notes encompass topics in data science and civil services. The beauty of knowledge is that you don’t need to belong to any specific group; simply maintain your curiosity, and knowledge will find its way to you. I hope these notes are helpful. If they are, please consider leaving a comment below and follow my blog for updates.” Mathematics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Revision Statistics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11

Maths 1 week 1 Summary

Number System and Set Theory 📚 Number System and Set Theory This week, our teacher covered the basics of the number system. We were instructed to consider 0 as part of the natural numbers, as it will be treated as such in future subjects like Python. However, in exams, it will be explicitly stated whether 0 should be considered a natural number. The key topics from this week include set theory and the relationship between two sets. In set theory, we focused on three Venn diagram problems. In the context of relations, we discussed the concepts of reflexive, symmetric, transitive, and equivalence relations. Detailed Explanation 1.Union of Two Sets The union of two sets A and B is the set of elements that are in either A , B , or both. It is denoted as A ∪ B . 2.Intersection of Two Sets The intersection of two sets A and B is the set of elements that are in both A and B . It is denoted as A ∩ B . 3.Subt

Community page

Welcome To our IITM BS Students Community This community is a student commune where IIT Madras Bachelor of Science students are studying. Our community is managed by 15 community admins who oversee our WhatsApp community, Discord, and Telegram profiles. With more than 1000+ active members, we study together, share memes, watch movies, play games, and have fun. Our goal is to bring all online IITM students together to excel in exams while having fun. Community Admins Agampreet LinkedIn Ansh Ashwin Ambatwar Arti Dattu Dolly Elango Koushik Shrijanani Saksham Shivamani Shivam Instagram LinkedIn Join Our Community Subscribe to our YouTube page Join our meme team on