Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:44:29+00:00 2026-06-02T22:44:29+00:00

Sample Input : car truck 8 4 bus 6 1 Sample Output : bus

  • 0

Sample Input : “car truck 8 4 bus 6 1”

Sample Output : “bus car 1 4 truck 6 8”

If the nth element in the string is an integer it must remain an integer,
and if it is a word it must remain a word

Is there a more elegant and efficient way of doing this? Below is my ruby code. I am looking for code which is more efficient(need not be in ruby).

puts "Enter Input:"
inp = gets

inp_ary=inp.split(" ")

a=inp_ary.group_by{|i| i=~ /\d/}

sort_words = a[nil].sort
sort_integer = a[0].sort

index_words=[]
index_integer=[]

inp_ary.each_with_index do |e,i|
  if e =~ /\d/
    index_integer << i
  else
    index_words << i
  end
end

final = []

sorted = sort_words + sort_integer

index_integer.each_with_index do |e,i|
  final[e] = sort_integer[i]
end

index_words.each_with_index do |e,i|
  final[e] = sort_words[i]
end

puts "Sorted Output: "
puts final.join(" ")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:44:32+00:00Added an answer on June 2, 2026 at 10:44 pm
    s = "car truck 8 4 bus 6 1"
    
    tokens = s.split
    numbers, words = tokens.partition{|t| t =~ /^\d+$/}
    numbers.map!(&:to_i).sort!
    words.sort!
    
    result = tokens.map{|t| (t =~ /^\d+$/ ? numbers : words).shift}.join(' ')
    
    p result
    #=> "bus car 1 4 truck 6 8"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sample input string: char *str = 12345.567675; And the desired output if I need
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
Sample input: I was 09809 home -- Yes! yes! You was and output: {
I am stuck at one more RegEx. Sample input : project description I want
sample input (orgtext = a[crlf]b[space]c[crlf] ) I like to store each word a,b, c
I hope to get help here one more time. Here is my sample input
I hope to get help here one more time. Here is my sample input
My sample input XML is: <root> <a> <b>item</b> <b>item1</b> <b>item2</b> <b>item3</b> <b>item4</b> </a> </root>
For example Input <url wiki=https://wiki.archlinux.org/index.php/Main_Page forums=https://bbs.archlinux.org/> Desired output <url wiki=https://wiki.archlinux.org/index.php/Main_Page forums=https://bbs.archlinux.org/> Here's a sample
I followed these two exemples of developer.android (Creating an Input Method, Soft Keyboard sample).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.