473,847 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

split number on their counts in python

1 New Member
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16, 17 and if 11314151617 (11cnt) then should be split like 1,13,1,15,16,17

I am trying to code like

if linecnt == 12 :
numList = [int(digit) for digit in str(strsplit)]

however i m getting 1,2,1,3,1,4,1,5 ,`,6 ..

guyz any help plz..
Feb 6 '24 #1
2 30917
645 Recognized Expert Contributor
I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16, 17 and if 11314151617 (11cnt) then should be split like 1,13,1,15,16,17
What you mean by count?
Mar 28 '24 #2
2 New Member
To split the number correctly based on your requirement, you should use a different approach that considers the count of digits in the number. Here's a Python function that does that:
Expand|Select|Wrap|Line Numbers
  1. def split_number(number, linecnt):
  2.     number_str = str(number)
  3.     result = []
  5.     if linecnt == 12:
  6.         for i in range(0, len(number_str), 2):
  7.             result.append(int(number_str[i:i+2]))
  8.     elif linecnt == 11:
  9.         i = 0
  10.         while i < len(number_str):
  11.             if i == 0 or i == 2:
  12.                 result.append(int(number_str[i]))
  13.                 i += 1
  14.             else:
  15.                 result.append(int(number_str[i:i+2]))
  16.                 i += 2
  18.     return result
  20. # Test cases
  21. number1 = 121314151617
  22. linecnt1 = 12
  23. number2 = 11314151617
  24. linecnt2 = 11
  26. print(split_number(number1, linecnt1))  # Output: [12, 13, 14, 15, 16, 17]
  27. print(split_number(number2, linecnt2))  # Output: [1, 13, 1, 14, 15, 16, 17]
2 Weeks Ago #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: nicolas | last post by:
Aim: scanf a number what random digit,split this number; for example: input "5680" output "5,6,8,0"; input "45" output "4,5";
by: Jay | last post by:
Hey Guys, I need an algorithm/formula to do the following: I have two 32-bit timers cascaded to form a 64-bit timer, max value per timer(50sec). This is the way they work: value | timer1 | timer2 5 50 3 when timer1 counts down to zero, timer2 would count down to two.
by: skip | last post by:
A guy at work asked for functionality commonly found with rational numbers, so I said I'd find and install something. I figured gmpy would be suitable, alas I'm having trouble successfully building the underlying GMP 4.2.1 library on a PC running Solaris 10 (won't compile with the default --host, fails "make check" if I go the no-assembly route). Before I invest a bunch of time into this, am I barking up the wrong tree? Performance is,...
by: Jason7899 | last post by:
hi, i have a number with 9 digits lenght and i want split this number: 922888222 into this: 922 888 222 how can i do that? thanks a lot for your help :)
by: Alex Hoilo | last post by:
Hello, I'm a novice to VBA and need to import raw data into an access database. I created the table, it contains 87 fields, all numeric. The data I have looks like this: 01390681068168120110301002936140400000000000000000 3000000100000000000000000000 0239068106816812011030100293603600541022867+000100 0+000000650+0000000000000067721292120000+0000000+0 000000+0000000+0000000+0000000+0000000+0000000+000...
by: yasirassassin | last post by:
hey sir i am having problem in splitting a number into its digits if i use remainder and division operation then i can split a number and print it in reverse order but dont know how to split that number and print that in correct order here is my program #include<iostream> using namespace std; int main() { int x=12345,r,c=1; while(c<=5){ r=x%10;
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.