Get the Middle Factors from a Factor List


I do a lot of work in number theory, especially with factors.
I often need just the two middle factors from a very long factor list/result, so I created a function that takes a factor list and returns just the middle two, or one, factors.

Nothing earth shattering, but I use it a lot, and thought that I would share it.

And those with better ways to offer, offer away. I am always interested.

About the Author

Science and Math Researcher (For Fun)

# Imports #
import sys, os, importlib, inspect, locale, time, math
from datetime import datetime
locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'

    # Main #
def main():

        # Main Code

    print("Middle Factors: {}".format(f_GetMiddleFactors([1, 7, 13, 91])))
    print("Middle Factors: {}".format(f_GetMiddleFactors([1, 7, 49])))


    # Functions #
        # Normal Functions

            # Get Middle Factor(s).
def f_GetMiddleFactors(v_FactorList):
    """Syntax: Takes LIST; Returns: LIST;
    Desc.: Takes a factor LIST and returns a LIST of the middle factor(s).
    Test: print("Middle Factors: {}".format(f_GetMiddleFactors([1, 7, 13, 91])))"""
    v_MiddleFactors = [] ## Init v_MiddleFactors list
    v_LengthList = len(v_FactorList) ## Get init factor list length
    if v_LengthList % 2 == 0: ## If list length is even
        v_MiddleFactors.append((str(v_FactorList[((v_LengthList/2)-1)]))) ## Extracts and appends to v_MiddleFactors the middle-left factor.
        v_MiddleFactors.append((str(v_FactorList[(v_LengthList/2)]))) ## Extracts and appends to v_MiddleFactors the middle-right factor.
    else: ## If list length is odd
        v_MiddleFactors.append((str(v_FactorList[((v_LengthList/2))]))) ## Extracts and appends to v_MiddleFactors the middle factor.
    return v_MiddleFactors

    # Main Loop #
if __name__ == '__main__':
Gribouillis 1,391 Programming Explorer Team Colleague

Here is how you can code the same thing by using a PEP8 compliant coding style

# -*-coding: utf8-*-
# python 2 or 3

def get_middle_factors(factor_list):
    """Return a list of the term(s) in the middle of a list

    A list of length 1 or 2 is returned, depending on the
    length of the argument list being odd or even.


        >>> get_middle_factors([1, 7, 13, 91])
        [7, 13]
        >>> get_middle_factors([1, 7, 91])
    n = len(factor_list)
    q, r = divmod(n-1, 2)
    return factor_list[q:q+1+r]

def main():
    print("Middle Factors: {}".format(get_middle_factors([1, 7, 13, 91])))
    print("Middle Factors: {}".format(get_middle_factors([1, 7, 49])))

if __name__ == '__main__':

""" my output -->
Middle Factors: [7, 13]
Middle Factors: [7]

PEP 8 is good for your code as it makes it easier to read, especially by trained python programmers, who may not take the time to decrypt code written with a significantly different style.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

@BustACode we just want to help you with your coding style.
Python was written for readability, so yes PEP 8 is good to follow. See ...

Also, don't use unnecessary imports. They just clutter your code and are somewhat misleading.

Remember ...

import this

The Ninja IDE helps you with PEP 8 style:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.