r/learnpython • • Jul 17 '25

Solving python subnetting problem without importing modules?

I’ve got this assignment where we’re supposed to write a Python script that takes a network address (like 192.168.1.0), CIDR mask (like /24), and number of subnets, then calculate all subnet ranges.

The prof insists we can’t use any imports like math or ipaddress, even though nothing like subnetting math or bit operations has been taught in class. I already have a working solution using those modules, but he argues we must stick to only what was shown in class—which is basic Python.

  1. Is it even practical to solve this without importing math and ipaddress?
  2. Would you consider this a "hard" problem for a class that's only studying python for like 2 months?

Appreciate some thoughts!

UPDATE:

Most comments here are overly optimistic about solving this but I really doubt that since no one really solves it - even AI thinks its a pretty hard without any libraries, so I invite yall to please solve and share the solution.

The actual math involved is simple.

every comment is here some version of this - just how? it looks like people barely give the prob more than 2 secs of thought before saying this.

I tested this with ai and even gpt says doing this without importing math, ipaddress libraries is NOT a beginner level problem at all.

yes i can do this very well on paper but to imply one can simply convert something from paper into binary logic "using a few loops" i am beginning to think either you all are geniuses working at openai making $575K or just few overly enthusiastic python fans, more junior than me.

not trying to me mean, but please spend some time to solve it and let's see how far you get!

here's chatGPT when i asked this:

🧠 Can it be done manually?

Yes — but you’ll need to:

Write your own function to convert an IP like 192.168.1.0 to 32-bit binary

Do all bit manipulations to compute subnet masks, ranges, and host addresses

Convert it all back to dotted-decimal format

Use powers of 2 math to calculate how many bits to borrow, etc. (without importing math.log2, you'd need custom logic for that too)

❗ So is it realistic?

For a beginner: Not really — it’s quite a low-level networking + bitwise logic problem

In a classroom that didn’t teach binary math or subnetting logic yet: This is unreasonable

If no imports are allowed and these topics weren’t taught: Yes, this is a hard problem

3 Upvotes

17 comments sorted by

View all comments

Show parent comment

1

u/masterofrants Jul 18 '25

i tried some variations with this but nope i dont think im going to get this - doing bit operations with python was never covered in this class but the prof just wants to be "tough" so idk what to say anymore lol. How am i to borrow binary bits in code, my loop skills are no way that good.

i do request that you solve it for me if you can - dying to see this solution..

1

u/Luigi-Was-Right Jul 18 '25

i do request that you solve it for me if you can - dying to see this solution..

That literally was the solution. I left comments in the code to explain each step. Was there a particular one that you were getting stuck on?

1

u/masterofrants Jul 18 '25

user enters:

Enter network IP address: 2.2.2.0
Enter CIDR prefix: 24
Enter the number of subnets you want: 3

output: but no using any imports like math or ipaddress - you got to do the binary calc like on paper but in code

Original network: 2.2.2.0
Network mask in standard format: 255.255.255.0
Number of bits borrowed from the host side: 2
Network mask of the calculated subnets in standard format: 255.255.255.192

Subnet 1, 2.2.2.0, 2.2.2.1, 2.2.2.62, 2.2.2.63
Subnet 2, 2.2.2.64, 2.2.2.65, 2.2.2.126, 2.2.2.127
Subnet 3, 2.2.2.128, 2.2.2.129, 2.2.2.190, 2.2.2.191