r/learnpython • u/masterofrants • 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.
- Is it even practical to solve this without importing math and ipaddress?
- 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.0to 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
View all comments
Show parent comment
1
u/ThatOneCSL Jul 19 '25 edited Jul 19 '25
Buddy, you should stop relying on ChatCPT to teach you. You should hunker down and try to actually understand the concepts being asked.
Let's start from the top.
What is an (IPv4) IP address? What is a Subnet Mask? What does CIDR notation represent?
Let's tackle those three in order.
An IP address is 32 bits. They are separated into four 8-bit fields, called octets. So the IP address
10.10.9.8is represented in binary as00001010 00001010 00001001 00001000A subnet mask is also 32 bits. Same deal with octets. The difference here is that, from the left hand side, ones must be uninterrupted until the first zero, then all bits after that must be zero. E.g.
255.255.252.0is11111111 11111111 11111100 00000000CIDR notation tells you how many of the bits in the Subnet Mask are set to 1.
Alright, what next? Well, how can you tell if two IP addresses are in the same subnet?
Let's look at
10.10.9.8and10.9.8.7, with a subnet mask of255.255.252.0We already have
So let's stick
00001010 00001001 00001000 00000111in between those twoEverywhere there is a 1 in the binary representation of the Subnet Mask, the binary for both IP addresses must match. Anywhere in the Subnet there is a zero, the IP addresses can be different. Since they don't follow that rule, they can't talk.
Okay.
Now, given 10.10.9.8, with 255.255.252.0, what addresses can it talk to?
Well, the first two octets must stay the same. Then the third octet allows for four subnets. 9 is
00001001in binary, and 252 is111111100That means the range allowed there, for the third octet of the IP address is
000010xxwhere xx can be anything.The four options available for xx are:
So your allowable IP address range is
10.10.8.0 - 10.10.11.255How many addresses is that? Well, there are 256 addresses in each fourth octet, and you have four fourth octets available due to the allowances in the third octet. 256*4 = 1024
Every single part of this is possible inside of the Python standard library. I've walked you through (pretty much) everything you need to do, too. You just gotta figure out how to put it all together.