Visit Our UNIX and Linux User Community


Find the lights which are turn on.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the lights which are turn on.
# 1  
Old 07-01-2010
Find the lights which are turn on.

See the interview question from other site, that I'd like to implement by AWK.

The rules are:

There are 100 lamps, number from 1 to 100. Initially, all of them are returned off.
First time, if the number of lamp is multiple by one, switch the lamp status; that's mean, if it turn off, turn it on, vice versa.
Second time, if the number of lamp is multiple by two, switch the lamp status;
Third time, if the number of lamp is multiple by three, switch the lamp status;
And so on, until find out the number of lamp is multiples of 100, switch the lamp status;

Finally list all number of lamps which turn on.

I write the code, but seems the var in Function is not public var. array a 's value can't be used outside of function.

Code:
$ Cat my.awk

function s(n) {for (i=1;i<=100;i++) {a[i]=(a[i]%n)?a[i]:-a[i]}}
BEGIN{for (i=1;i<=100;i++) a[i]=1}
{for (i=1;i<=100;i++) s(i)}
END {for (i=1;i<=100;i++) {if (a[i]=="-1") print i, a[i]} }'

How can I fix it?
# 2  
Old 07-01-2010
Something like this?
Code:
awk '
BEGIN {
for(nL=1;nL<=100;nL++) 
   for(nS=1;nS<=100;nS++) 
      if  ( ! nL % nS ) a[nL]=a[nL]?0:1
for(nL=1;nL<=100;nL++)
   if ( a[nL] ) {print "Lamp:  "nL" --> Encendida"} 
}'

Code:
Lamp:  1 --> Encendida
Lamp:  4 --> Encendida
Lamp:  9 --> Encendida
Lamp:  16 --> Encendida
Lamp:  25 --> Encendida
Lamp:  36 --> Encendida
Lamp:  49 --> Encendida
Lamp:  64 --> Encendida
Lamp:  81 --> Encendida
Lamp:  100 --> Encendida

This User Gave Thanks to Klashxx For This Post:
# 3  
Old 07-01-2010
I don't know the answer, but it is interesting, your results are some regular numbers, all of them are square numbers.

When I run your code in my system, I don't get any output. Try to understand your idea first.
# 4  
Old 07-01-2010
Klashxx's result is correct. However, a more efficient way of calculating the result would be:
Code:
awk -v n=100 'BEGIN {while ((j=++i^2) <= n) print j}'

Smilie

If you think about the problem (forget about the code, just think about the math), every single time that a lamp's id number is divisible by a number x, there will be another number, y, less than or equal to the number of lamps, by which it will also be divisible. So, for each x that toggles the lamp's state, there is a y which will undo its effect ... except when x=y, a square root.

Regards,
Alister

Last edited by alister; 07-01-2010 at 03:00 PM..
This User Gave Thanks to alister For This Post:
# 5  
Old 07-02-2010
Very , very smart Alister !!

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Just can't turn off ipv6?

I'm running a Linux OS that uses Debian as it's base. A commercial vpn is installed that uses OpenVPN. For some reason, I can't get ipv6 to tunnel properly .... and Ipleak.net shows that my location is being unmasked by ipv6. I've tried kernel commands at boot, I've tried sysctl.conf commands.... (2 Replies)
Discussion started by: benc
2 Replies

2. Shell Programming and Scripting

turn list into one line

Hi All i am cat'ing a file and then using cut to get rid of some info, is there a way to turn the list into one line i.e 1 2 3 4 5 into 1 2 3 4 5 (4 Replies)
Discussion started by: ab52
4 Replies

3. Programming

Turn ON/OFF Wireless Card

Hi all, In my program, I am trying to use ioctl to turn on/off the wireless card, using SIOCSIFFLAGS (I am new to it but somebody said it is the most traditional way). However, it seems that I didn't set the flag correctly since the wireless is always on (I have root permission). If any one... (2 Replies)
Discussion started by: tetelee
2 Replies

4. Red Hat

Turn off subshelling of pipe

After searching the forum, I found that Bash spawns a subshell when using the pipe. I am wondering if there is an option or any other way to turn off this fuctionality. We are pulling over some scripts from a Korn shell environment. I have found ways to work around looping from a pipe (Thanks... (4 Replies)
Discussion started by: jryan
4 Replies

5. Shell Programming and Scripting

Turn Off the Message [YOU HAVE NEW MAIL]

#!/bin/sh cd /u02/app/ccalloc/dev/in CURDIR=`pwd` echo directory listing of $CURDIR echo ls -latr i have a basic shell script that displays a specific files on a directory. when the script is run and it does not find the file in the directory it display a first line message of how... (3 Replies)
Discussion started by: wtolentino
3 Replies

6. UNIX for Dummies Questions & Answers

turn off sound

how to disable anoying beep sound??? (4 Replies)
Discussion started by: nnn
4 Replies

7. SuSE

Amber Lights + Remote Servers

Ok, so we get reports from operations after they do walk throughs about servers with amber or red lights showing. They want us to resolve the issues. On Solaris I can run prtdiag to identify the amber lights and clear them (replace hardware or "reboot" the daemon). How do I remotely figure out... (1 Reply)
Discussion started by: BOFH
1 Replies

8. UNIX for Dummies Questions & Answers

How to turn off duplex

I am using digital Unix and lpd. I have HP 4200n LaserJet TCP printer, but when I use lpr command, it always print duplex. I can turn off duplex feature at the panel of the printer, but then other Windows computer cannot print duplex. How can I set up /etc/printcap file so that it will be... (2 Replies)
Discussion started by: hiepng
2 Replies

9. SuSE

RH8.0 firewall WILL NOT turn off

I have been trying to disable the firewall on a new install of RH8(Psyche). It will NOT stay disabled. I've gone thru system tools, security level and disabled it, and it says YES, like it will save my settings, but when i open it up again, it is always back to HIGH. I also tried using the... (3 Replies)
Discussion started by: kymberm
3 Replies

Featured Tech Videos