Carry Counts

Hello all!

It’s been a while since I’ve been able to post, but I wanted to cover another Bloomsburg problem from 2018 for my programming club.  The problem description is this:

When calculating the sum of two numbers by hand, we first add the digits in the 1’s position, and if the result overflows (i.e., is greater than 9) then we carry the leftmost 1 in the result to the 10’s position. This process is repeated at the 10’s position, then at the 100’s position, and so on. For example, if we are calculating 1523 + 817 by hand, we write:

In this case, there are two carries (shown in circles).

Write a program that prompts the user for two positive integers and outputs the number of carries that would occur when performing addition by hand.

The following test cases illustrate the required I/O format.

We’ll begin this problem by defining our variables and taking in some input.  Our input will be the two integers we will be adding, and then we will be putting them into a list.

carries = 0
int1 = int(raw_input("Enter two positive integers to be added: "))
int2 = int(raw_input())
ints = [int1, int2]

Next up we need to define our algorithm we will be using.   For this problem we will be using a new function called zfill(). The zfill() function fills a string up with zeroes on the left side until a specific length is reached.  We’ll also be using the .max() method to find the maximum value of the list, and then run the len() method on it to get the max length.  Doing this we can fill up both integers until they are the same length, allowing me to then work backwords through them, adding each value from right to left and counting the carries for each value.

length = len(max(ints))
add1 = int1.zfill(length)
add2 = int2.zfill(length)

Then we’ll use a for loop to loop through the length of the integers, moving from right to left and adding each value.  If the summed value is greater than 9, then the value will have a carry.  We also need to create a variable called extra, which I will use as the actual carry, because in a case like 89+11 the carry from the ones value causes the tens value to also increase by one, so we must account for it.  Otherwise if the summed value is less than or equal to nine, we do nothing and set extra equal to zero.

for i in range(length):
    if int(add1[-(i+1)]) + int(add2[-(i+1)]) + extra >=10:
        extra = 1
        carries += 1
    elif int(add1[-(i+1)]) + int(add2[-(i+1)]) + extra <= 9:
        extra = 0

After we’ve iterated through the integers, we just need to check how many carries there were and print our statements accordingly for grammatical correctness.

if carries == 1:
    print("There will be 1 carry.")
elif carries <= 0:
    print("There will be no carries.")
    print("There will be ", carries, " carries")

And now we have a working solution to Bloomsburg 2018 Problem 3, Carry Counts.  I hope this helps. 🙂

Thanks for reading and have a wonderful day!
~ Corbin


Hello all!

This post is a bit off the normal topics I cover here, but I feel it will be helpful in the future for both me and others to reference to.  What I’d like to talk about here is an area that I run at BSides Delaware (And soon some other conferences!) called Spawncamp. Here I want to cover what Spawncamp is, why I feel you should care, and how to create your own for a conference you run or help out at.

Some background, I started going to conferences when I was 6, the first conference I went to was BSides Delaware.  Conferences like these really helped define a lot of my life, and I owe a lot of my passion and learning to the people I’ve met at them.  When my father brought me there, there was no real place set for kids, meaning a lot of what I did was running between different villages like the wireless village and the lockpicking village.  Don’t get me wrong, this was fantastic and helped me a lot because I learned awesome things like lockpicking and SDR hacking at a really young age.  But it wasn’t organized, and it was a lot of work for me to get involved into the community fully, so a few years later I helped ‘spawn’ Spawncamp to alleviate this.

The main goal of spawncamp is to provide a safe place for kids at conferences to learn and enrich themselves in whatever they’re interested in.  It started off with just Computer Science and information security, but I’ve been working to expand topics.  Such as last year (2018) I gave an impromptu class on music theory, and we taught tons of kids and adults chess.  A big thing I’ve noticed from running Spawncamp is that hands on or tangible activities are very attractive to attendees, so topics such as Arduino programming, chess, and 3D Printing hold attention very well.  I always try to keep things interesting and make sure nobody is just sitting there bored with nothing to do, hence my impromptu music theory class in 2018.  In Spawncamp there are 2 tracks, structured and unstructured.  The structured track consists of the main classes and talks that other volunteers and I put on such as a Python class or a Wireshark class.  The unstructured track is where we have activities such as destruction alley, snap circuits kits, or just even legos.  It’s intended to be a place where the kids can hang out and do other activities if there isn’t a class or talk they’re interested in.  So far Spawncamp has improved its content range every year and continues to help kids learn about whatever they’re passionate about.

Why should you care? What if you don’t have kids?  Well adults can learn too! We had some students from the college we are hosted by come into a wireshark talk and they were surprised to have learned a good amount.  And if you do have kids, then hopefully it’s obvious why you should care.  If your kid is interested in a field, then hopefully one of the volunteers will be able to help them either learn more, or maybe explore other fields that the kid is unsure of.  That’s the beauty of Spawncamp, it can be whatever you want!

But, I hear you say, “How do I start a Spawncamp?” Well it’s not that hard, first of course you need a conference or event to host it at, but after you’ve figured that out you can just expand off the base goal into whatever form you want.  I like the form that we’ve come up with at BSides Delaware, which consists of a structured and unstructured track.  In the structured track we place whatever talks and classes we have for that year, they change a lot but a few good ideas are usually Arduino, Python, soldering, 3D Printing, Circuit / Electronics Design.  Then in the unstructured track we have other activities such as destruction alley (where old electronics can be taken apart by the kids and then recycled), chess, potato batteries, and snap circuits.  These activities are hands on and meant to captivate the attention of kids between classes or other activities.

Some pro tips we’ve learned:

      • Use a tarp for destruction alley to prevent damage to floors and losing screws
      • Safety goggles are a MUST HAVE for destruction alley
      • Hands on activities are great, but always have supervision from atleast one other person than the instructor!

I hope this post helps anyone who wants to start a Spawncamp or was interested in determining if it was something they should participate in.

Thanks for reading and have a wonderful day!
~ Corbin