Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Forum Statistics

Users
Members:
543
Members Online:
8
Guests Online:
7

Total Post History
Posts:
45705
Topics:
9398

7-Day Post History
New Posts:
343
New Topics:
81
Active Topics:
90

Our newest member
sangeeta

Other

FAQ
All times are UTC [ DST ]

Google Ads

LTD Puzzle 7: format bytes and add postfixes

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

LTD Puzzle 7: format bytes and add postfixes

Postby chrissie1 on Fri Jul 18, 2008 10:27 am

Given a numerical input of bytes, create a function to format the output to two decimal places along with the relevant postfixes

e.g.
  1. Input: 54831             Expected Output: 53.55 kilobytes
  2. Input: 1384120           Expected Output: 1.32 megabytes
  3. Input: 24568971464       Expected Output: 22.88 gigabytes
  4. Input: 7392649371023     Expected Output: 6.72 terabytes


For clarification in this puzzle we would have to define whether the increments are in 1000's or 1024's and define each postfix like so:

  1. Kilobytes
  2. Megabytes
  3. Gigabytes
  4. Terabytes
  5. Petabytes
  6. Exabytes
  7. Zettabytes
  8. Yottabytes


So we want the correct and HD-manufacturers result ;-).
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 7: format bytes and add postfixes

Postby b3orn on Fri Jul 18, 2008 11:12 am

Am I really understood the puzzle?

Code is hidden, SHOW


This is the output:
  1. 53.55 kilobytes
  2. 1.32 megabytes
  3. 22.88 gigabytes
  4. 6.72 terabytes
b3orn
Apprentice
Apprentice
LTD Bronze - Rating: 37
 
Posts: 9
Joined: Thu Jul 17, 2008 3:23 pm

Re: LTD Puzzle 7: format bytes and add postfixes

Postby chrissie1 on Fri Jul 18, 2008 11:21 am

I think you did.

Don't worry, next week will be a difficult one ;-)
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 7: format bytes and add postfixes

Postby b3orn on Fri Jul 18, 2008 1:17 pm

Am I really understood the puzzle?

But what I wrote above? I definitely need an english course ...
b3orn
Apprentice
Apprentice
LTD Bronze - Rating: 37
 
Posts: 9
Joined: Thu Jul 17, 2008 3:23 pm
Unrated

Re: LTD Puzzle 7: format bytes and add postfixes

Postby spoulson on Fri Jul 18, 2008 3:50 pm

Here is a C# 3.0 method that takes advantage of anonymous types.

Code is hidden, SHOW


Incase you can't tell, I'm a big fan of the ternary operator.
User avatar
spoulson
Senior Apprentice
Senior Apprentice
LTD Bronze - Rating: 112LTD Bronze - Rating: 112LTD Bronze - Rating: 112
 
Posts: 203
Joined: Mon Jun 02, 2008 2:37 am
Location: Middletown, DE, USA

Re: LTD Puzzle 7: format bytes and add postfixes

Postby Emtucifor on Fri Jul 18, 2008 6:27 pm

Code is hidden, SHOW

Code is hidden, SHOW
It's more cheesier if you do it right the first time!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California

Re: LTD Puzzle 7: format bytes and add postfixes

Postby SeanJA on Fri Jul 18, 2008 10:22 pm

In keeping with my inefficient ruby coding, here is my ungainly answer:

Code is hidden, SHOW


It does however return the right things, which is good. I think I should probably redo it with a loop...

  1. #puts "kilo"
  2. 54831.bytes
  3. #puts "mega"
  4. 1384120.bytes
  5. #puts "giga"
  6. 24568971464.bytes
  7. #puts "terra"
  8. 7392649371023.bytes
  9. #puts "peta"
  10. (1023**6).bytes
  11. #puts "zetta"
  12. (1025**7).bytes
  13. #puts "yotta"
  14. (1077**8).bytes
  15. #puts "Wow!"
  16. (1077**9).bytes
  17.  


  1. 53.55 Kilobytes
  2. 1.32 Megabytes
  3. 22.88 Gigabytes
  4. 6.72 Terrabytes
  5. 1018.01 Petabytes
  6. 1.01 Zettabytes
  7. 1.5 Yottabytes
  8. Wow, I don't know what to call that!
  9.  
SeanJA
Newbie
Newbie
LTD Bronze - Rating: 18
 
Posts: 3
Joined: Thu Jul 17, 2008 7:31 am

Re: LTD Puzzle 7: format bytes and add postfixes

Postby chrissie1 on Fri Aug 01, 2008 7:54 am

There are 2 types of winner - a "People's Champion" (the one with the most rating points across all their posts) and the LTD Admin's Champion (selected with our special formula....honest...well, maybe it's just the ones that the admins thought were particularly good :-))

So...

People's Champion: b3orn scoring 14 points for his answer

LTD Admin's Champion: SeanJA for having the uggliest code (being original is always a bonus on LTD).


Congratulations to all.. and good luck on next weeks puzzle :-)
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 7: format bytes and add postfixes

Postby Emtucifor on Fri Aug 01, 2008 5:06 pm

Terrabytes.

ugh! :)
It's more cheesier if you do it right the first time!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California
Unrated

Re: LTD Puzzle 7: format bytes and add postfixes

Postby tisodotsk on Fri Aug 08, 2008 3:33 pm

My PHP solution:
Code is hidden, SHOW

Output:
  1. 53.55 Kilobytes
  2. 1.32 Megabytes
  3. 22.88 Gigabytes
  4. 6.72 Terabytes
I try to improve my English language skills. Most things i do better than this.
tisodotsk
Apprentice
Apprentice
LTD Bronze - Rating: 59LTD Bronze - Rating: 59
 
Posts: 22
Joined: Fri Aug 08, 2008 12:45 pm
Location: Bratislava, Slovakia
Unrated