find closest palindrome date of a given palindrome date [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
A palindrome date is given and we have to find the closest palindrome date of this given date. The result date should be before the given date.
Date is in the format of YYYY/MM/DD.
One brute force solution is (pseudo-code is given which might not be efficient).
Convert the string into a number
For each number less than this number
If the number is a valid date then
If number is a palindrome
print and return
I think there must be some efficient algorithm to solve this problem.
Example: Input date: 2030/03/02
Output date: 2021/12/02

If you consider that a palindrome date is written as YYYY/MM/DD, like 2121/12/12, you can make some conclusions:
A day in the month is between 01 and 31 and a month between 01 and 12. So you deduce that a date palindrome matches the following regex:
([0-9][0-2]|[01]3)([0-9]0|[12]1)-(0[0-9]|1[12])-([0-2][0-9]|3[01])
1. & 2. 3 & 4 4 & 3 2 & 1
I have marked the four letters which repeat
The palindrome before the inputted date would be
decrementing if possible n°4 (last year and ten month before)
Or
decrementing if possible number n°3 (10 years and 1 month before)
Or
decrementing if possible number n°2 (100 years and 10 days before)
Or
decrementing if possible number n°1 (1000 years and 1 day before)
Or
Stop

In the given year, there will be only one palindromic date. Hence, to find previous palindromic date, you need to decrement the year and convert it to palindromic date.
Example: for 2030, valid month and day will be 03 and 02 which will lead to 2030/03/02.
So, it must be clear that palindromic date in this case is only possible when:
Year is of 4 digits (not for 10000) as months and number of days are in two digits
Last two digits of years should be {10, 20, 30, 40, 50, 60, 70, 80, 90, 01, 11, 21}
First two digits of years should be mirror image of 1-31 (with month and year validity).
Using rule 2 and 3, it can be checked in O(1) whether a given year has palindromic date or not in the given format. Let us assume that valid range of year is from 9999-0001, then it can be done in linear order to find previous palindromic date.
void find_prev_date(const char* input, char * output) {
int year = extract_year(input); //Extract first 4 digits
--year;
//limit is 101 because 0000 will not give you valid month and day.
for(;year>=101;--year) {
if(haspalindromic(year))
{
write_date(year, output);
}
}
}
bool haspalindromic(int year)
{
int first2 = year%100;
int last2 = year/100;
bool second_valid = false;
bool first_valid = false;
switch(last2){
case 10: case 20: case 30: case 40: case 50: case 60: case 70: case 80:
case 90: case 1: case 11: case 21: second_valid = true; break;
}
if(!second_valid)
return false;
switch(first2) {
case 10: case 20: case 30: case 40: case 50: case 60: case 70: case 80: case 90: case 1: case 11: case 21: case 31: case 41: case 51: case 61: case 71: case 81: case 91:
case 2: case 12: case 22: case 32: case 42: case 52: case 62: case 72: case 82: case 92:
case 3: case 13:
first_valid = true;
break;
}
if(!first_valid)
return false;
//reverse digit such that 2 yields 20
int month = reverse_digits_on100(last2);
int day = reverse_digits_on100(first2);
if(month==2) {
if(day<=28)
return true;
if(isleapyear(year) && day==29)
return true;
if(day>28)
return false;
}
switch(month) {
case 4: case 6: case 9: case 11:
if(day>30)
return false;
}
//For remaining months, check is already made as first part of year is gives day in range of [1..31].
return true;
}
Some functions are not implemented, but their names clearly suggest intents.

Related

OAMulator (print highest score of 7)

I am trying to write a program in OAMulator that is reflective of this Pseudo-code.
Set highest to 0
Set count to 7
While count > 0
Get newScore
Print newScore
If newScore > highest
Set highest to newScore
Set count to count - 1
Print highest
Stop
This is what I've come up with. We have to use 7 scores and get the highest and print only the highest score. We also have to use a count loop. I entered 0 as a control to start the inputs along with 7 other scores. TO BE CLEAR I AM NOT LOOKING FOR THE AWNSER OR SOLUTION. i am looking to learn why this isnt working and how to make a count loop work with a higherthan loop.
INPUT LIST
0
60
70
80
90
83
76
95
Then I wrote the code as best as I could, but the issue I run into is the count and new score run off the Accumulator. I tried writing another loop to change it but then the two loops don't always run and the count gets messed up or the scores don't get tallied right.
SET 7 #set count to 7
count, STA 100 #store count to slot 100
loopstart, LDA 0 #start loop, load 0
STA 101 #store 0 in slot 101
LDA 0 #load 60
STA 102 #store in slot 102
LDA 101 #load 60
SUB 102 #subract 70
BRP loopstart #if pos then loop, if not go on.
LDA 100 #load count
DEC # count -1
BRP count #if count <0 loop to count
print, STA 102 #print highest
HLT
here is the trace and memory
TRACE
0: PC=1 IR=[?????] AR=? ACC=? B=?
1: PC=2 IR=[SET 7] AR=1 ACC=7 B=?
2: PC=3 IR=[STA 100] AR=100 ACC=7 B=?
3: PC=4 IR=[LDA 0] AR=0 ACC=0 B=?
4: PC=5 IR=[STA 101] AR=101 ACC=0 B=?
5: PC=6 IR=[LDA 0] AR=0 ACC=60 B=?
6: PC=7 IR=[STA 102] AR=102 ACC=60 B=?
7: PC=8 IR=[LDA 101] AR=101 ACC=0 B=?
8: PC=9 IR=[SUB 102] AR=102 ACC=-60 B=60
9: PC=10 IR=[BRP 3] AR=9 ACC=-60 B=60
10: PC=11 IR=[LDA 100] AR=100 ACC=7 B=60
11: PC=12 IR=[DEC] AR=11 ACC=6 B=60
12: PC=2 IR=[BRP 2] AR=12 ACC=6 B=60
13: PC=3 IR=[STA 100] AR=100 ACC=6 B=60
14: PC=4 IR=[LDA 0] AR=0 ACC=70 B=60
15: PC=5 IR=[STA 101] AR=101 ACC=70 B=60
16: PC=6 IR=[LDA 0] AR=0 ACC=80 B=60
17: PC=7 IR=[STA 102] AR=102 ACC=80 B=60
18: PC=8 IR=[LDA 101] AR=101 ACC=70 B=60
19: PC=9 IR=[SUB 102] AR=102 ACC=-10 B=80
20: PC=10 IR=[BRP 3] AR=9 ACC=-10 B=80
21: PC=11 IR=[LDA 100] AR=100 ACC=6 B=80
22: PC=12 IR=[DEC] AR=11 ACC=5 B=80
23: PC=2 IR=[BRP 2] AR=12 ACC=5 B=80
24: PC=3 IR=[STA 100] AR=100 ACC=5 B=80
25: PC=4 IR=[LDA 0] AR=0 ACC=90 B=80
26: PC=5 IR=[STA 101] AR=101 ACC=90 B=80
27: PC=6 IR=[LDA 0] AR=0 ACC=83 B=80
28: PC=7 IR=[STA 102] AR=102 ACC=83 B=80
29: PC=8 IR=[LDA 101] AR=101 ACC=90 B=80
30: PC=9 IR=[SUB 102] AR=102 ACC=7 B=83
31: PC=3 IR=[BRP 3] AR=9 ACC=7 B=83
32: PC=4 IR=[LDA 0] AR=0 ACC=76 B=83
33: PC=5 IR=[STA 101] AR=101 ACC=76 B=83
34: PC=6 IR=[LDA 0] AR=0 ACC=95 B=83
35: PC=7 IR=[STA 102] AR=102 ACC=95 B=83
36: PC=8 IR=[LDA 101] AR=101 ACC=76 B=83
37: PC=9 IR=[SUB 102] AR=102 ACC=-19 B=95
38: PC=10 IR=[BRP 3] AR=9 ACC=-19 B=95
39: PC=11 IR=[LDA 100] AR=100 ACC=5 B=95
40: PC=12 IR=[DEC] AR=11 ACC=4 B=95
41: PC=2 IR=[BRP 2] AR=12 ACC=4 B=95
42: PC=3 IR=[STA 100] AR=100 ACC=4 B=95
Error: Attempt to read missing input
43: PC=4 IR=[LDA 0] AR=0 ACC=? B=95
Abort: 43 instructions executed.
MEMORY
1. SET 7
2. STA 100
3. LDA 0
4. STA 101
5. LDA 0
6. STA 102
7. LDA 101
8. SUB 102
9. BRP 3
10. LDA 100
11. DEC
12. BRP 2
13. STA 102
14. HLT
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100. 4
101. 76
102. 95
I figured it out. It took me hours. almost days.
SET 8 #set count to 8
STA 100 #store in slot 100
SET 0
STA 102 #slot 0 as highest
loopstart, LDA 100 #Load count
DEC #count -1
BRZ print # if count is 0 print
STA 100 # if count is more than 1 store
LDA 0 #load ACC
STA 101 #store ACC in 101
LDA 101 #load Number
SUB 102 #subract highest
BRP while
BR loopstart
print, LDA 102
STA 0
HLT
while, LDA 101
STA 102 #if ACC is positive store as highest
BR loopstart
There doesn't seem to be much help on the internet for OAM assembly language so i posted the answer for others in the future.

C Array acting strange when it pass the number of arguments

I was implementing Command-line arguments with C , And I shocked when the Arguments Array start acting strange when it passes the number of the argument given. here is the code :
#include <stdio.h>
int main(int a, char *arra[]){
/*
for(int i = 1;i<a;i++){
printf("Arguments N%d -> %s\n",i+1,arra[i]);
}
printf("Totale Arguments:%d\n",a-1);
*/
for(int i = 0;i<=49 ;i++){
printf("%d: -> %s\n--------------------------------------
------\n",i,arra[i]);
}
return 0;
}
My question is how did the array have access to the Linux Terminal variable like the Path variable PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
And the Output was like :
0: -> ./list
--------------------------------------------
1: -> hi
--------------------------------------------
2: -> 234
--------------------------------------------
3: -> hello
--------------------------------------------
4: -> (null)
--------------------------------------------
5: -> LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
--------------------------------------------
6: -> XDG_MENU_PREFIX=gnome-
--------------------------------------------
7: -> LANG=en_US.UTF-8
--------------------------------------------
8: -> GDM_LANG=en_US.UTF-8
--------------------------------------------
9: -> DISPLAY=:1
--------------------------------------------
10: -> COLORTERM=truecolor
--------------------------------------------
11: -> USERNAME=root
--------------------------------------------
12: -> XDG_VTNR=2
--------------------------------------------
13: -> SSH_AUTH_SOCK=/run/user/0/keyring/ssh
--------------------------------------------
14: -> XDG_SESSION_ID=7
--------------------------------------------
15: -> USER=root
--------------------------------------------
16: -> DESKTOP_SESSION=gnome
--------------------------------------------
17: -> GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/bbdf3fc7_241a_437d_86d0_982594cbb92d
--------------------------------------------
18: -> PWD=/root/Documents/Programming/c
--------------------------------------------
19: -> HOME=/root
--------------------------------------------
20: -> SSH_AGENT_PID=1581
--------------------------------------------
21: -> QT_ACCESSIBILITY=1
--------------------------------------------
22: -> XDG_SESSION_TYPE=x11
--------------------------------------------
23: -> XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/
--------------------------------------------
24: -> XDG_SESSION_DESKTOP=gnome
--------------------------------------------
25: -> GJS_DEBUG_OUTPUT=stderr
--------------------------------------------
26: -> GTK_MODULES=gail:atk-bridge
--------------------------------------------
27: -> WINDOWPATH=2
--------------------------------------------
28: -> TERM=xterm-256color
--------------------------------------------
29: -> SHELL=/bin/bash
--------------------------------------------
30: -> VTE_VERSION=5202
--------------------------------------------
31: -> XDG_CURRENT_DESKTOP=GNOME
--------------------------------------------
32: -> GPG_AGENT_INFO=/run/user/0/gnupg/S.gpg-agent:0:1
--------------------------------------------
33: -> GNOME_TERMINAL_SERVICE=:1.67
--------------------------------------------
34: -> SHLVL=1
--------------------------------------------
35: -> XDG_SEAT=seat0
--------------------------------------------
36: -> GDMSESSION=gnome
--------------------------------------------
37: -> GNOME_DESKTOP_SESSION_ID=this-is-deprecated
--------------------------------------------
38: -> LOGNAME=root
--------------------------------------------
39: -> DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
--------------------------------------------
40: -> XDG_RUNTIME_DIR=/run/user/0
--------------------------------------------
41: -> XAUTHORITY=/run/user/0/gdm/Xauthority
--------------------------------------------
42: -> PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
--------------------------------------------
43: -> GJS_DEBUG_TOPICS=JS ERROR;JS LOG
--------------------------------------------
44: -> SESSION_MANAGER=local/FXMACO:#/tmp/.ICE-unix/1529,unix/FXMACO:/tmp/.ICE-unix/1529
--------------------------------------------
45: -> _=./list
--------------------------------------------
46: -> OLDPWD=/root/Documents/Books
--------------------------------------------
47: -> (null)
--------------------------------------------
Segmentation fault
for(int i = 0;i<=49 ;i++){
as we see here your argument list is shorter than 50 elements
It is good to use standard names and to take into the account number of arguments passed (that is the reason of the argc)
int main(int argc char *argv[])
{
for(int i = 0;i < argc ;i++){

Spiral outwards on jagged 2D grid

My question is very similar to these other questions:
Looping in a spiral
On a two dimensional grid is there a formula I can use to spiral coordinates in an outward pattern?
However, what do you do if your grid/matrix is irregular?
I'm creating a game where there are certain 'seats', represented by a 2D grid. On every odd row, there's one less seat/cell. When rendered, those rows are offset by ½ seat. I need an algorithm that outputs the closest seats, relative to whatever seat-coordinate I input, in an descending order, like so (blue cell is the starting coordinate, semi-transparent cells are outside the grid) :
The seat grid is stored as a jagged, multidimensional array, so the previous visualization is a bit misguiding. From an "algorithmic" point of view, it actually looks more like this (again, blue cell is the starting coordinate, semi-transparent cells are outside the array bounds):
The output would be something like
[0,0][1,0][0,1][-1,1][-1,0][-1,-1][0,-1][1,-1][2,0][1,1]...
Here's an iterative approach that breaks the spiral into 7 sub-levels per loop of the spiral, one sub-level to move out from the previous spiral level and 6 sub-levels to trace a hexagonal path around the boundary of the previous level:
static void spiralLoop(int startx, int starty, int levels)
{
int level = 1, sublevel = 0, sublevelstep = 0;
int x = startx, y = starty;
while(level <= levels)
{
System.out.println("["+x+","+y+"]");
switch(sublevel)
{
case 0:
x++; // stepping up from previous (next innermost) loop
break;
case 1:
x+=(y&1);y++; // up and right
break;
case 2:
x-=(~y&1);y++; // up and left
break;
case 3:
x--; // left
break;
case 4:
x-=(~y&1);y--; // down and left
break;
case 5:
x+=(y&1);y--; // down and right
break;
case 6:
x++; // right
break;
default:
break;
}
if(sublevel == 0) // (3)
sublevel = 1;
if(++sublevelstep >= level) // (1)
{
sublevelstep = 0;
if(++sublevel > 6) // (2)
{
level++;
sublevel = 0;
}
}
}
}
(1) The length of each hexagonal side (number of sub-level steps) is equal to the level (which starts from one). After each iteration the number of steps is incremented, and if it has reached the end of a sub-level the sub-level is incremented and steps are reset to 0.
(2) If the level has been completed (sub-level > 6) then the level is incremented and sub-level is reset to 0.
(3) The first sub-level of each level (moving up from the previous level) only lasts one iteration.
It doesn't do any checking for whether the current position is outside the grid, but that would be simple to add.
A start x,y position is passed in, and used to initialize x and y. These values are used to determine whether the current row is odd or even, which affects how the position is updated.
To move diagonally left, decrement x only when y is even. To move diagonally right, increment x only when y is odd. See below:
row 0: 0 1 2 3 4 5 6 7 8 9
row 1: 0 1 2 3 4 5 6 7 8
row 2: 0 1 2 3 4 5 6 7 8 9
row 3: 0 1 2 3 4 5 6 7 8
row 4: 0 1 2 3 4 5 6 7 8 9
row 5: 0 1 2 3 4 5 6 7 8

Changes in array of coprime numbers

I'm given an array and have to determine the minimum number of additions(ADDITION BY 1) that have to be done to the elements such that they are all no longer coprime(GCD>1)
I could recursively check if the gcd is >1. If it is not, I thought that the easiest GCD to get would be 2, so I can add 1 to all the odd numbers and get the GCD as 2 however this fails for cases with very few/no even numbers and where it is easier to generate other GCDs such as 11, 33, 55 and 76. Here I can add 1 to 76 and change the GCD from 1 to 11 in 1 step rather than changing 11,33 and 55 by making 3 additions and getting GCD as 2.
I wanted to know the most efficient way of doing this. Also checking the GCD of the array would be O(nlogn).
You could find the prime factorization of each element, then count how many elements have each prime in the factorization. The number of additions required is [Element count] - [MAX(Prime Count)].
For example, the factorization for [11, 33, 55, 76] is:
11: 11^1
33: 3^1 * 11^1
55: 5^1 * 11^1
76: 2^2 * 19^1
The prime counts are:
2: 1
3: 1
5:1
11: 3
19: 1
The highest count is 3, there are 4 elements, so you need 1 addition (4 - 3). If you need to know which elements need to be added to, it's the elements whose factorization don't include the max counted prime.

while loop continues not breaking up

I have made a code where I have to
input
1 . no of questions
2. difficulty for every question
3 . no. of queries( query is to find median of difficulty questions between questions no. given in next line)
4 . question no.s to find median
loop goes on till no input is given .
#include<stdio.h>
int main()
{
int ques,count=0;
while(scanf("%d",&ques))
{
int i,diff,se,fi,j,query,arr[100];
for(i=0;i<ques;i++)
{
scanf("%d",&diff);
arr[i] = diff;
}
count++;
printf("Case %d:\n",count);
scanf("%d",&query);
for(i=0;i<query;i++)
{
int sum = 0;
scanf("%d %d",&fi,&se);
for(j=fi-1;j<se;j++)
{
sum = sum+ arr[j];
}
sum = sum/((se-fi)+1);
printf("%d\n",sum);
}
}
return 0;
}
Here i give two inputs
5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
4
1 3
2 4
3 5
2 3
6
2 56 2 3 5 4
1
2 5
but my output should be upto case 3 only instead:
Case 1:
3
3
2
Case 2:
6
6
4
5
Case 3:
16
Case 4:
4
Case 5:
4
Case 6:
4
Case 7:
4
Case 8:
4
Case 9:
4
Case 10:
4
Case 11:
4
Case 12:
4
Case 13:
4
Case 14:
4
Case 15:
4
and it goes on and on : Tell me why is this happening:
You need to check the return value of scanf, if you give control-D, it will return -1, in your program that means you need to quit, but -1 will cause the while condition be true.
Whwnever you are ending , no more values in there in your input . it returns -1 ; but your code seems to take '0' as quit which is making -1 to be true and your code is running .

Resources