I had a written test of a company the other day, there's an
interesting problem. it's as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
....
n
n-1
....
i
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.
Can anybody figure out if it's feasible without loosing any points?
Thanks, 28 1333
<gt*******@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...
>I had a written test of a company the other day, there's an
interesting problem. it's as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
...
n
n-1
...
i
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.
Can anybody figure out if it's feasible without loosing any points?
Thanks,
#include <stdio.h>
void idiotic_thing(int i, int n);
void idiotic_thing(int i, int n)
{
idiotic_thing1(i, n);
}
void idiotic_thing1(int i, int n)
{
int j;
if (i < n) return;
for (j = i; j <= n; j++)
printf("%d\n", j);
for (j = n-1; j >= i; j++)
printf("%d\n", j);
return;
}
The function idiotic_thing itself uses only one statement and no loop.
Army1987 wrote:
<gt*******@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...
I had a written test of a company the other day, there's an
interesting problem. it's as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
...
n
n-1
...
i
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.
Can anybody figure out if it's feasible without loosing any points?
Thanks,
#include <stdio.h>
void idiotic_thing(int i, int n);
ITYM, void idiotic_thing1(int i, int n);
void idiotic_thing(int i, int n)
{
idiotic_thing1(i, n);
}
void idiotic_thing1(int i, int n)
{
int j;
if (i < n) return;
This check squashes the purpose of this routine.
for (j = i; j <= n; j++)
printf("%d\n", j);
for (j = n-1; j >= i; j++)
printf("%d\n", j);
return;
}
The function idiotic_thing itself uses only one statement and no loop.
gt*******@gmail.com said:
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.
Can anybody figure out if it's feasible without loosing any points?
A loop is an iteration-statement, which is one of the several different
kinds of statement defined by the grammar, so I see no reason why it
should lose any points. When your questioner can produce this:
*
* *
* *
* *
* *
* *
*
in a single bodyless iteration-statement (where the height of the
diamond is configurable at runtime), I'll think about thinking about
his problem. Until then, just tell him he's behind the curve.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
<gt*******@gmail.comwrote in message
>I had a written test of a company the other day, there's an
interesting problem. it's as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
...
n
n-1
...
i
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.
Can anybody figure out if it's feasible without loosing any points?
Think recursion.
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
In article <11**********************@p15g2000hsd.googlegroups .com>, gt*******@gmail.com <gt*******@gmail.comwrote:
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
Here are the main ingredients:
expression statement
comma operator
conditional expression
function call expression
the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure
out how to combine them to get the desired result.
Lauri
"santosh" <sa*********@gmail.comha scritto nel messaggio
news:11**********************@o5g2000hsb.googlegro ups.com...
>void idiotic_thing1(int i, int n) { int j; if (i < n) return;
This check squashes the purpose of this routine.
Whoops... I meant (i n) (but I think it's unnecessary anyway...)
"Lauri Alanko" <la@iki.fiha scritto nel messaggio
news:eu**********@oravannahka.helsinki.fi...
Here are the main ingredients:
expression statement
comma operator
conditional expression
function call expression
the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure
out how to combine them to get the desired result.
Also, think about the behaviour of && and || when the second operand has
side effects.
"Army1987" <pl********@for.itwrote in message
news:eu**********@tdi.cu.mi.it...
>
"Lauri Alanko" <la@iki.fiha scritto nel messaggio
news:eu**********@oravannahka.helsinki.fi...
>Here are the main ingredients:
expression statement comma operator conditional expression function call expression the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure out how to combine them to get the desired result.
Also, think about the behaviour of && and || when the second operand has
side effects.
No && or || are required.
"Barry" <ba****@nullhighstream.netha scritto nel messaggio
news:13*************@corp.supernews.com...
>Also, think about the behaviour of && and || when the second operand has side effects.
No && or || are required.
There is more than one way to do that. I used && and ||, but used no ternary
operator.
On Apr 1, 2:46 pm, "Army1987" <please....@for.itwrote:
"Barry" <bar...@nullhighstream.netha scritto nel messaggionews:13*************@corp.supernews.com.. .
Also, think about the behaviour of && and || when the second operand has
side effects.
No && or || are required.
There is more than one way to do that. I used && and ||, but used no ternary
operator.
But did you check the return value of printf and handle
errors gracefully?
On 1 Apr, 22:06, "Bill Pursell" <bill.purs...@gmail.comwrote:
There is more than one way to do that. I used && and ||, but used no ternary
operator.
But did you check the return value of printf and handle
errors gracefully?
Ehm... no, but if that matters, you can include the right headers, set
errno to 0, call the function, and then check errno...
Anyway, I never used printf as the left operator of &&, if you meant
*that*. In the function I wrote, it is always used as the left
"operand" of a comma.
Barry wrote:
"Army1987" <pl********@for.itwrote in message
>"Lauri Alanko" <la@iki.fiha scritto nel messaggio
>>Here are the main ingredients:
expression statement comma operator conditional expression function call expression the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure out how to combine them to get the desired result.
Also, think about the behaviour of && and || when the second operand has side effects.
No && or || are required.
Neither is the comma operator nor printf (for n < 10). putc will
suffice.
void doit(int n) {
if (n) {
putc(n + '0'); putc('\n');
doit(n - 1);
if (n 1) {
putc(n + '0'); putc('\n');
}
}
}
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com ar******@email.it writes:
On 1 Apr, 22:06, "Bill Pursell" <bill.purs...@gmail.comwrote:
There is more than one way to do that. I used && and ||, but used
no ternary operator.
But did you check the return value of printf and handle errors gracefully?
Ehm... no, but if that matters, you can include the right headers, set
errno to 0, call the function, and then check errno...
[...]
Or, better yet, you can check the value returned by printf. The standard
doesn't say that printf set error on errors.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson <ks***@mib.orgwrites:
ar******@email.it writes:
>On 1 Apr, 22:06, "Bill Pursell" <bill.purs...@gmail.comwrote:
>There is more than one way to do that. I used && and ||, but used no ternary operator.
But did you check the return value of printf and handle errors gracefully?
Ehm... no, but if that matters, you can include the right headers, set errno to 0, call the function, and then check errno...
[...]
Or, better yet, you can check the value returned by printf. The standard
doesn't say that printf set error on errors.
Whoops, I meant that the standard doesn't say that printf sets *errno*
on errors.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Apr 1, 8:14 pm, Lauri Alanko <l...@iki.fiwrote:
In article <1175423465.991499.286...@p15g2000hsd.googlegroups .com>,
gteddy...@gmail.com <gteddy...@gmail.comwrote:
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
Here are the main ingredients:
expression statement
comma operator
conditional expression
function call expression
the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure
out how to combine them to get the desired result.
Lauri
Thanks, i think i've figured out how to do that.
int func(int i, int n)
{
return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d
\n", i);
}
The func works as required :) gt*******@gmail.com wrote:
On Apr 1, 8:14 pm, Lauri Alanko <l...@iki.fiwrote:
>In article <1175423465.991499.286...@p15g2000hsd.googlegroups .com>,
gteddy...@gmail.com <gteddy...@gmail.comwrote:
>>For example, let i = 1, n = 4, then the output should be: 1 2 3 4 3 2 1 The trickest thing is that you should USE ONLY ONE STATEMENT to accomplish that.
Here are the main ingredients:
expression statement comma operator conditional expression function call expression the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure out how to combine them to get the desired result.
Lauri
Thanks, i think i've figured out how to do that.
int func(int i, int n)
{
return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d
\n", i);
}
I guess that 'p' must be read as 'func'.
The func works as required :)
But according to the norm, this is not a statement (6.8) but a
function-definition (6.9.1). While
for (int i = 1; i < 2*n; i++)
printf("%d\n", i < n ? i : 2*n-i);
is a statement. And there is many other ways to write it with a single
statement.
a+, ld.
Laurent Deniau wrote:
gt*******@gmail.com wrote:
>On Apr 1, 8:14 pm, Lauri Alanko <l...@iki.fiwrote:
>>In article <1175423465.991499.286...@p15g2000hsd.googlegroups .com>,
gteddy...@gmail.com <gteddy...@gmail.comwrote: For example, let i = 1, n = 4, then the output should be: 1 2 3 4 3 2 1 The trickest thing is that you should USE ONLY ONE STATEMENT to accomplish that. Here are the main ingredients:
expression statement comma operator conditional expression function call expression the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure out how to combine them to get the desired result.
Lauri
Thanks, i think i've figured out how to do that. int func(int i, int n) {
return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d \n", i); }
I guess that 'p' must be read as 'func'.
>The func works as required :)
But according to the norm, this is not a statement (6.8) but a
function-definition (6.9.1). While
for (int i = 1; i < 2*n; i++)
printf("%d\n", i < n ? i : 2*n-i);
Sorry, I hadn't read the first post where you say that 'i' may not
necessary start at 1. So here a better version:
for (int j = 2*n-i; i <= j; i++)
printf("%d\n", i < n ? i : 2*n-i);
is a statement. And there is many other ways to write it with a single
statement.
a+, ld.
Laurent Deniau wrote:
>>>gteddy...@gmail.com <gteddy...@gmail.comwrote:
>>>>The trickest thing is that you should USE ONLY ONE STATEMENT to accomplish that.
Sorry, I hadn't read the first post where you say that 'i' may not
necessary start at 1. So here a better version:
for (int j = 2*n-i; i <= j; i++)
printf("%d\n", i < n ? i : 2*n-i);
>is a statement.
It's a statement, but it isn't ONE statement: I see two.
--
Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.
"Go not to the Drazi for counsel, Unsaid /Babylon 5/
for they will answer both 'green' and 'purple'."
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
<gt*******@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
On Apr 1, 8:14 pm, Lauri Alanko <l...@iki.fiwrote:
>In article <1175423465.991499.286...@p15g2000hsd.googlegroups .com>,
gteddy...@gmail.com <gteddy...@gmail.comwrote:
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
Here are the main ingredients:
expression statement comma operator conditional expression function call expression the standard function 'printf'
If you aren't familiar with some of them, look them up. Then figure out how to combine them to get the desired result.
Lauri
Thanks, i think i've figured out how to do that.
int func(int i, int n)
{
return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d
\n", i);
}
The func works as required :)
When I replied no && or || was required I was thinking:
void doitr(int i, int n)
{
i<n?printf("%d\n",i), doitr(i+1,n), printf("%d\n",i):printf("%d\n",i);
}
Chris Dollin wrote:
Laurent Deniau wrote:
>>>>gteddy...@gmail.com <gteddy...@gmail.comwrote:
>>>>>The trickest thing is that you should USE ONLY ONE STATEMENT to >accomplish that.
>Sorry, I hadn't read the first post where you say that 'i' may not necessary start at 1. So here a better version:
for (int j = 2*n-i; i <= j; i++) printf("%d\n", i < n ? i : 2*n-i);
>>is a statement.
It's a statement, but it isn't ONE statement: I see two.
It is one statement which includes an iterative-statement which includes
an expression... 'statement' definition in the norm is recursive. If set
A includes set B, it doesn't mean that A is two sets ;-)
a+, ld.
Chris Dollin wrote:
Laurent Deniau wrote:
.... snip ...
>
> for (int j = 2*n-i; i <= j; i++) printf("%d\n", i < n ? i : 2*n-i);
is a statement.
It's a statement, but it isn't ONE statement: I see two.
Picking miniscule nits, it IS one statement, a 'for' statement.
Yes, that statement is compounded of various other statements, yet
it remains a single statement. It's execution can be controlled by
a single preceding if (clause), etc. After which the if () etc.
would form a single statement.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
CBFalconer wrote:
Chris Dollin wrote:
>Laurent Deniau wrote:
... snip ...
>>
>> for (int j = 2*n-i; i <= j; i++) printf("%d\n", i < n ? i : 2*n-i);
is a statement.
It's a statement, but it isn't ONE statement: I see two.
Picking miniscule nits, it IS one statement, a 'for' statement.
Yes, that statement is compounded of various other statements, yet
it remains a single statement.
The `for` statement is one statement. But there is another statement
there -- the expression-statement calling printf. (If it were not a
statement, then the `for` would be illegal.)
I see two statements, one of which happens to be a component of the
other.
--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
Nit-picking is best done among friends.
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
Neither is the comma operator nor printf (for n < 10). putc will
suffice.
void doit(int n) {
if (n) {
putc(n + '0'); putc('\n');
doit(n - 1);
if (n 1) {
putc(n + '0'); putc('\n');
}
}
}
You meant putchar(), or putc(..., stdin)?
And that is not *one* statement.
"Army1987" <pl********@for.itha scritto nel messaggio
news:ev**********@tdi.cu.mi.it...
>Neither is the comma operator nor printf (for n < 10). putc will suffice.
void doit(int n) { if (n) { putc(n + '0'); putc('\n'); doit(n - 1); if (n 1) { putc(n + '0'); putc('\n'); } } }
You meant putchar(), or putc(..., stdin)?
And that is not *one* statement.
It is one statement. Sorry...
Thanks, i think i've figured out how to do that.
int func(int i, int n)
{
return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d
\n", i);
}
If i is 4 and n is 3, it does print 4.
Is it what you meant?
(The one I wrote prints nothing if n < i).
"Army1987" <pl********@for.itha scritto nel messaggio
news:ev**********@tdi.cu.mi.it...
>Neither is the comma operator nor printf (for n < 10). putc will suffice.
void doit(int n) { if (n) { putc(n + '0'); putc('\n'); doit(n - 1); if (n 1) { putc(n + '0'); putc('\n'); } } }
You meant putchar(), or putc(..., stdin)?
And that is not *one* statement.
Now I've read the other posts in this thread. It is one statement, but there
are several statements in it. (Nested statements are statements, too. So
this isn't what everybody would call "a function using one statement".
<OT>When I was about 8, once I drew a square divided in four, and asked
several people "How many squares can you see?". Most people anwswered "four"
(the small ones). Almost nobody answered "one". The answer I expected was
"five" (the small ones and the big one).</OT>)
Here is why *this* meaning of "one statement" sounded brain-dead to me.
With this, I could use if (1) to make any arbitrarily complex function "use
one statement". What I think on reading "function using one statement" is a
function with one semicolon in its body.
Army1987 wrote:
>
>Neither is the comma operator nor printf (for n < 10). putc will suffice.
void doit(int n) { if (n) { putc(n + '0'); putc('\n'); doit(n - 1); if (n 1) { putc(n + '0'); putc('\n'); } } }
You meant putchar(), or putc(..., stdin)?
And that is not *one* statement.
Yes, should be putchar. Nothing in the article to which I replied
said anything about one statement. The problem was to print:
N
N-1
....
1
2
....
N
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
--
Posted via a free Usenet account from http://www.teranews.com
On Apr 1, 10:31 pm, "gteddy...@gmail.com" <gteddy...@gmail.comwrote:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1
void func(int i, int n)
{
printf("%d\n", i), i < n && (func(i + 1, n), printf("%d\n", i));
}
Least inelegant correct answer so far? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Vandervies |
last post by:
I just fixed a bug that some of the correctness pedants around here may
find useful as ammunition.
The problem was that some code would, very occasionally, die with a
segmentation violation...
|
by: Bruno R. Dias |
last post by:
Perhaps it would be interesting to program a virtual machine simulating
an ancient computer (such as the pdp-7). Then, it would be rather
interesting to code for it (porting gcc to it maybe?). I...
|
by: David Sworder |
last post by:
Hi,
I'm developing an application that will support several thousand
simultaneous connections on the server-side. I'm trying to maximize
throughput. The client (WinForms) and server communicate...
|
by: Rakesh Roberts |
last post by:
I think I have a very interesting cookie problem.
I use form authentications on my application. Through out my
application I started using a toggle control that persists its value
for the session...
|
by: sasifiqbal |
last post by:
Hi,
One of my developers are facing an interesting problem regarding
UserControl invalidation.
The problem is:
We have two forms in our application. Form A does nothing except
loading of...
|
by: Frederick Gotham |
last post by:
I thought it might be interesting to share experiences of tracking down a
subtle or mysterious bug. I myself haven't much experience with tracking
down bugs, but there's one in particular which...
|
by: per9000 |
last post by:
An interesting/annoying problem.
I created a small example to provoke an exception I keep getting.
Basically I have a C-struct (Container) with a function-pointer in it.
I perform repeated calls...
|
by: Will Honea |
last post by:
I've hit an interesting trap trying to migrate data off an OS/2 server
running version 7.2 (fp14) over to 8.2 on Linux. Seems that one table has
a column defined in the DDL as "BIGINT NOT NULL...
|
by: onkar.n.mahajan |
last post by:
Is it possible to from function call on fly in C programming
language ?
I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database...
|
by: Andrew |
last post by:
I am having an interesting namespace conflict. When we use a third party
lib we create a company assembly for any descending classes to go in. I
have simplified the problem into the example...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |