Skip to main content
Spelling fix.
Source Link
ovs
  • 60.6k
  • 3
  • 47
  • 161

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

Problem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabyte" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • The following post-fixespostfixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not required to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled ones (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using [standard methods].(http://meta.codegolf.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods)

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

Problem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabyte" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • The following post-fixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not required to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled ones (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using [standard methods].(http://meta.codegolf.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods)

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

Problem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabyte" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • The following postfixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not required to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled ones (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using [standard methods].(http://meta.codegolf.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods)

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

Problem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabytle""gigabyte" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • The following postfixespost-fixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not required to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled ones (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using [standard methods].(standardhttp://meta.codegolf.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods methods)

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

Problem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabytle" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • The following postfixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not required to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled ones (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using standard methods

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

Problem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabyte" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • The following post-fixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not required to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled ones (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using [standard methods].(http://meta.codegolf.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods)
Improved formatting, tweaked wording
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

UserProblem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabytle" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • FollowingThe following postfixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not expectedrequired to work with negative numbers
  • Do not use any external libraries (e.g. BCMathBCMath in PHP) besides bundled ones (e.g. math.h math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using standard methods

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

UserProblem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabytle" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • Following postfixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not expected to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using standard methods

Background

The task is simple but every programmer has implemented it at least once. Stackoverflow has a lot of samples, but are they short enough to win?

Problem story

You're a grumpy programmer who was given the task of implementing file size input for the users. Since users don't use bytes everyone will just enter "1M", "1K", "3G", "3.14M" - but you need bytes! So you write a program to do the conversion.

Then your manager hands you dozens of user reports with complaints about weird large numbers in file size input. It seems you'll need to code the reverse conversion too.

What do you need to do?

Here's the trick: you should implement two-way conversion in single piece of code. Two separate functions used as needed? Nah, that's too simple - let's make one, short one!

For the purposes of this challenge, "kilobyte" means 1024 bytes, "megabyte" means 10241024 = 1048576 bytes, and "gigabytle" means 10241024*1024 = 1073741824 bytes.

Test data

Input   -> Output  
5       -> 5  
1023    -> 1023  
1024    -> 1K  
1K      -> 1024  
1.5K    -> 1536  
1536    -> 1.5K  
1048576 -> 1M  
1M      -> 1048576  

Rules

  • Test value will not exceed 210241024*1024 or 2G
  • The following postfixes are used by users: K for kilobytes, M for megabytes, G for gigabytes
  • Code is not required to work with negative numbers
  • Do not use any external libraries (e.g. BCMath in PHP) besides bundled ones (e.g. math.h)
  • Standard loopholes are disallowed
  • Code should not produce anything on stderr
  • Your program can take input and produce output using standard methods
added 13 characters in body
Source Link
kiler129
  • 235
  • 1
  • 4
Loading
Edited for grammar and clarity
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141
Loading
minor typo (s/Code Gold/Code Golf/)
Source Link
kiler129
  • 235
  • 1
  • 4
Loading
s/k/K/
Source Link
kiler129
  • 235
  • 1
  • 4
Loading
added 45 characters in body
Source Link
kiler129
  • 235
  • 1
  • 4
Loading
edited body; edited title
Source Link
kiler129
  • 235
  • 1
  • 4
Loading
edited body
Source Link
SuperJedi224
  • 12k
  • 1
  • 45
  • 68
Loading
Source Link
kiler129
  • 235
  • 1
  • 4
Loading