Jump to content

Say that I have a list of [9,4,'10',5,1,'7','2',6,'3','8']. If I run the following function on that list 

def arbitrary_sort(alist : list) -> list:
    return(sorted((sorted(alist, key=(lambda x: x if type(x) is int else False))), key=(lambda x: int(x) if type(x) is str else False), reverse = True))
    

I'll get an output of ['10', '8', '7', '3', '2', 1, 4, 5, 6, 9] but the output SHOULD be [1, 4, 5, 6, 9, '10', '8', '7', '3', '2']. I feel like I'm almost there, but I can't see where I'm going wrong. Can I get some sort of hint as to how I can essentially have the order of ints first, and then the strings? 

 

Note that I'm restricted to just writing out the answer in one line for this question. 

Link to comment
https://linustechtips.com/topic/845819-arbitrary-sorting-in-python/
Share on other sites

Link to post
Share on other sites

I smell codewars

brb

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

Why not just do: 

def arbitrary_sort(list):
	return sorted(list)

 

EDIT: nvm....

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

13 minutes ago, TallOne123 said:

-snip-

Let me know if this works:

def arbsort(lst):
    return sorted([i for i in lst if type(i) is int])+reversed([s for s in lst if type(s) is str])

Edit: I know it won't... let me fix it. But I think you can see where I'm going with it.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

22 minutes ago, TallOne123 said:

Say that I have a list of [9,4,'10',5,1,'7','2',6,'3','8']. If I run the following function on that list 


def arbitrary_sort(alist : list) -> list:
    return(sorted((sorted(alist, key=(lambda x: x if type(x) is int else False))), key=(lambda x: int(x) if type(x) is str else False), reverse = True))
    

I'll get an output of ['10', '8', '7', '3', '2', 1, 4, 5, 6, 9] but the output SHOULD be [1, 4, 5, 6, 9, '10', '8', '7', '3', '2']. I feel like I'm almost there, but I can't see where I'm going wrong. Can I get some sort of hint as to how I can essentially have the order of ints first, and then the strings? 

 

Note that I'm restricted to just writing out the answer in one line for this question. 

def arbsort(lst):
    intlist = sorted([i for i in lst if type(i) is int])
    strlist = [str(st) for st in reversed(sorted([int(s) for s in lst if type(s) is str]))]
    returnable = intlist+strlist
    print returnable
arbsort([1, 2, 3, 5, 7, 4, 2, '1', '6', '3', '2'])

Modify that and then mark this post as the answer to the thread :)

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×