Skip to main content
Notice removed Content dispute by CommunityBot
Post Unlocked by CommunityBot
removed modifications to the question that invalidate answers or are based on said invalidations
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

SOLUTION 1 GZ0-Mod:

    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] ```

UPDATE: Ran a performance benchmark - results for iterations = 1e7, and a, b, c = tuple, list, dict of length 10, nested, respectively:

  • #1: Solution 1 GZ0-Mod -- 114.2 sec, 71.2% faster than slowest
  • #2: Solution 2   ---------- 131.9 sec, 48.0% faster than slowest
  • #3: Solution 1   ---------- 155.7 sec, 25.4% faster than slowest
  • #4: Gloweye's lambda  --- 160.5 sec, 21.7% faster than slowest
  • #5: RootTwo's functools -- 195.3 sec

Note: these differences are negligible in practice - and if otherwise, timing variance is substantial, and all are about on par (per noted benchmark)


SOLUTION 1 GZ0-Mod:

    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] ```

UPDATE: Ran a performance benchmark - results for iterations = 1e7, and a, b, c = tuple, list, dict of length 10, nested, respectively:

  • #1: Solution 1 GZ0-Mod -- 114.2 sec, 71.2% faster than slowest
  • #2: Solution 2   ---------- 131.9 sec, 48.0% faster than slowest
  • #3: Solution 1   ---------- 155.7 sec, 25.4% faster than slowest
  • #4: Gloweye's lambda  --- 160.5 sec, 21.7% faster than slowest
  • #5: RootTwo's functools -- 195.3 sec

Note: these differences are negligible in practice - and if otherwise, timing variance is substantial, and all are about on par (per noted benchmark)

Post Locked by Vogel612
Notice added Content dispute by Vogel612
Rollback to Revision 20
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

SOLUTION 1 GZ0-Mod:

def ordered_shuffle(*args):
    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] 

SOLUTION 1 GZ0-Mod:

    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] ```

SOLUTION 1 GZ0-Mod:

def ordered_shuffle(*args):
    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] 

SOLUTION 1 GZ0-Mod:

    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] ```
deleted 3 characters in body
Source Link

SOLUTION 1 GZ0-Mod:

    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] ```

SOLUTION 1 GZ0-Mod:

def ordered_shuffle(*args):
    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] 

SOLUTION 1 GZ0-Mod:

    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] ```

SOLUTION 1 GZ0-Mod:

def ordered_shuffle(*args):
    arg_types = map(type, args)
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(arg_types, zip(*zipped_args))] 
Rollback to Revision 18
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141
Loading
deleted 4 characters in body
Source Link
Loading
deleted 57 characters in body
Source Link
Loading
Indicate that the GZ20 solution is not for review
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419
Loading
added 15 characters in body
Source Link
Loading
added 76 characters in body
Source Link
Loading
added 6 characters in body
Source Link
Loading
added 660 characters in body
Source Link
Loading
Tweeted twitter.com/StackCodeReview/status/1179229741859168256
Became Hot Network Question
added 1 character in body
Source Link
Loading
added 82 characters in body
Source Link
Loading
edited tags
Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141
Loading
added 290 characters in body
Source Link
Loading
deleted 11 characters in body
Source Link
Loading
added 103 characters in body
Source Link
Loading
deleted 84 characters in body
Source Link
Loading
edited tags
Link
AlexV
  • 7.4k
  • 2
  • 24
  • 47
Loading